开发者

pass by pointer is not working

开发者 https://www.devze.com 2023-01-04 02:26 出处:网络
#include\"iostream\" class CMessage { public:int a; CMessage(){} ~CMessage(){} }; void Testing(CMessage *f_pMessage)//l_pMessage is output parameter
#include"iostream"
class CMessage
{
public:int a;
       CMessage(){}
       ~CMessage(){}
};
void Testing(CMessage *f_pMessage)//l_pMessage is output parameter
{
    f_pMessage = new CMessage();
    f_pMessage->a = 1;
}
int main()
{
    CMessage *l_pMessage =NULL;
    Testing(l_pMessage);
    std::cout<<l_pMessage->a;//getting l_pMessage = NULL;
    return 0;
}

When I called testing then inside testing f_pMessage is getting initialized but as soon as i after excuting testing function it should be store i开发者_StackOverflow中文版n l_Pmessage but it is showing NULL.confussed.....


Testing(l_pMessage);

At this line, you are passing a copy of the pointer. You either need to pass a pointer to pointer or a reference to pointer:

void Testing(CMessage *& f_pMessage)//l_pMessage is output parameter
{
    f_pMessage = new CMessage();
    f_pMessage->a = 1;
}

You can do it the other way using a pointer to pointer:

void Testing(CMessage **f_pMessage)//l_pMessage is output parameter
{
    *f_pMessage = new CMessage();
    (*f_pMessage)->a = 1;
}

But you have to call the function this way:

Testing(&l_pMessage);


Passing by pointer only allows you to modify what is being pointed at. The pointer itself is still being passed by value.

Since you want to change a pointer, you can either pass a pointer to a pointer or take the pointer by reference:

void Testing(CMessage *&f_pMessage)//l_pMessage is output parameter
{
    f_pMessage = new CMessage();
    f_pMessage->a = 1;
}


The pointer itself is copied by value.

f_pMessage = new CMessage();

modifies the pointer itself, not the object it points to. Try:

void Testing(CMessage *f_pMessage)
{
    f_pMessage->a = 1;
}
int main()
{
    CMessage *l_pMessage = new CMessage();
    Testing(l_pMessage);
    std::cout<<l_pMessage->a;
    return 0;
}

Or, if you want to create the object in your function:

CMessage *Testing()
{
    CMessage *f_pMessage = new CMessage();
    f_pMessage->a = 1;
    return f_pMessage;
}
int main()
{
    CMessage *l_pMessage = Testing();
    std::cout<<l_pMessage->a;
    return 0;
}


A pointer to a pointer is the way to go or a pointer reference

void testA(int** var)
{
    *var = new int;
    **var = 3;
}

void testB(int*& var)
{
    var = new int;
    *var = 3;
}

Use like this:

// Method a
int* a;
testA(&a);
std::cout << "a: " << *a << "\n";

// Method b
int* b;
testB(b);
std::cout << "b: " << *b << "\n";

// Dont forget to delete!
delete a;
delete b;

I hope this explains it.


The answers I have seen are correct, but they have omitted the most important point I think: you should not use a raw pointer, because you don't have any ownership semantics here.

void Testing(std::unique_ptr<CMessage>& message)
{
  message = std::unique_ptr<CMessage>(new CMessage());
  message->a = 1;
}

int main(int argc, char* argv[])
{
  std::unique_ptr<CMessage> l_pMessage;
  Testing(l_pMessage);
  std::cout << l_pMessage->a;
  return 0;
}

This is better because it uses proper semantics to indicate within the code who is responsible for the allocated object, and use the type system to have the compiler enforce correct use.

Now, I would advise another interface:

std::unique_ptr<CMessage> Testing()
{
  return std::unique_ptr<CMessage>(new CMessage(1));
}

int main(int argc, char* argv[])
{
  std::unique_ptr<CMessage> l_pMessage = Testing();
  std::cout << l_pMessage->a;
  return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消