-1

Possible Duplicate:
C++: Passing pointer variable to function

Here is a simplified code snippet which I think shows the problem at hand:

std::wstring *Variable3 = &SomeWStringObject; int _tmain(int argc, _TCHAR* argv[]) { std::wstring *Variable1 = NULL; func(Variable1); } void func(std::wstring *Variable2) { Variable2 = Variable3; } 

Now, in reality, func is a member of a class, and Variable3 is also a member of that same class. For this simpler example, let's just assume that Variable3 is some sort of global variable.

Variable3 is a (global variable) pointer to a std::wstring object. I can see in the debugger than it is pointing to the correct string.

What I want to end up with is Variable1 pointing to the same std::wstring object as Variable3.

So I tried passing the address of pointer Variable1 into the function, which I hoped would then set the address Variable3 points to into Variable1.

But this isn't working. It seems to be set OK, but then when the program leaves func, Variable1 is still a null pointer.

I have tried to be as clear as I can. I hope it is enough. Unfortunately, I cannot use the return value of func for this, as I actually have two other std::wstringstream objects to do the same thing to. Since all are having the same problem, I simplified it down to just one std::wstring object. I have tried lots of other different combinations of & and *, but none have worked.

Thank you very much for any help you can offer.

7
  • 1
    If you want a function (in this case, func) to be able to change the value of variables it was passed, use references: Declare it as void func(std::wstring *& Variable2). Commented Dec 22, 2012 at 14:03
  • You copied a pointer Variable1 to Variable2 and assigned a value to Variable2. Why should Variable1 change? Commented Dec 22, 2012 at 14:05
  • This isn't an answer. Commented Dec 22, 2012 at 14:07
  • Thank you all so much for such a great response. Commented Dec 22, 2012 at 14:18
  • Why should I change my answer? It is absolutely correct. You've simply assumed, you've understood the problem. I have not. I just asked a question (after short explatation) to be sure, I've understood the problem, and niemiro understood what he/she did. Commented Dec 22, 2012 at 19:17

3 Answers 3

4

Pass the pointer by reference:

void func(std::wstring *& Variable2) 

Though there are better ways to accomplish what you're doing as suggested by the other answers. Using pointers in this situation is quite superfluous.

Sign up to request clarification or add additional context in comments.

2 Comments

and do not use std::wstring. utf8everywhere.org
While this solves the immediate problem, this isn’t a good solution. Don’t modify arguments. Use return values.
2

I’ll skip the intermediate steps. In C++ you’d do it like this:

std::string mystring; std::string func() { return mystring; } int main() { std::string variable1 = func(); } 

A few remarks:

  • Lose the pointers. They don’t help here, they just add complexity.
  • To modify an argument, pass it by reference (but I haven’t done this).
  • Do not modify arguments. Return values instead.
  • Do not use std::wstring except to interface with the WinAPI.
  • If you need to share a reference to the same underlying string because you want to modify it transparently through the variable in main (and only then!), return a reference instead of a copy:

    std::string& func() { return mystring; } 

    … but this is probably a bad idea anyway: if func is indeed a member function it shouldn’t generally expose inner modifiable state to the outside world, this is breaking several rules of good code design.

1 Comment

Thank you for this. I am just trying to get to grips with OOP in C++. This is just a first practice project, and so tips on how to make my code better, in these ways, are most appreciated.
1

At the moment you're copying the value of Variable1 (which is the null pointer value) into func and then you're modifying that copy. So the Variable1 outside func is not changed. The immediate solution for people who are a little pointer-prone is to add another level of indirection. Pass a pointer to your Variable1 and then modify it through that. Like so:

func(&Variable1); void func(std::wstring** Variable2) { *Variable2 = Variable3; } 

However, this is not very good style. C++ has reference types that allow you to pass an object into a function without copying it, so that the object inside the function is the same as the object outside. We can use it like so:

func(Variable1); void func(std::wstring*& Variable2) { Variable2 = Variable3; } 

Variable2 is a reference to a pointer (denoted by the &). It means that when you pass Variable1, it is not copied and Variable2 refers to precisely the same pointer. Modifying Variable2 will modify Variable1 too.

The next question you should ask yourself is "Do I need a pointer to std::wstring in the first place?" I can't answer that for you, but often the answer is no.

And then the neeext question is why you are passing output through a parameter. This is a very C-ish thing to do (but even they wouldn't do it in this situation). This is the reason we have return values. Ideally, your code would look like this:

std::wstring Variable3 = SomeWStringObject; int _tmain(int argc, _TCHAR* argv[]) { std::wstring Variable1 = func(); } std::wstring func() { return Variable3; } 

Or potentially you will want to return a reference to Variable3, in whicih case your func return type should be std::wstring&.

6 Comments

+1 but this is only the beginning. The next step is to lose the pointer entirely. The next step is to remove the parameter and return a copy (or reference) instead of modifying an argument. The next step is to get rid of the global (or class member) pointer.
Thank you for such a detailed response. I was attempting (unsuccessfully) to add a second layer of indirection, and so your comments that references would be better here were greatly appreciated.
I have marked this as the answer not because it was any more correct than the others, but because the extra detail was appreciated.
-1: After reading your answer carefully, I should say it's more confuse then help. First, why using Ref to Ptr at all? void func(std::wstring& Variable2); func(SomeWStringObject); would do it fine. Second, your second example makes Variable1 pointing to the data of SomeWStringObject, your 3rd example not, so they are not equivalent. And third thing, never return reference of anything that is not non-pointer member of the same class (Variable3). Return only const references. Otherwise use shared pointer or auto_ptr depending on use-case.
@ValentinHeinitz "void func(std::wstring& Variable2); func(SomeWStringObject); would do it fine." But that's not the problem they're trying to solve. SomeWStringObject doesn't really have anything to do with it. They just want to pass a pointer to Variable3 out of the function func.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.