Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about f(MyClass *& mc) and f(new MyClass)

Status
Not open for further replies.

modemer

Technical User
Feb 16, 2005
22
US
compile following code is OK:
<code>
class MyClass
{
public:
int i;
};

void f(MyClass *& mc) {;}

int main()
{
f(new MyClass);
return 0;
}
</code>

but the following is not OK, why?
<code>
class MyClass
{
public:
int i;
};

void f(MyClass *& mc) {;}

int main()
{
MyClass m;
f(&m);
return 0;
}
</code>
 
Try:
Code:
...
void f(MyClass *mc){;}
...
MyClass *m;
f(m);
...

A pointer to the object is all you should need to modify it directly. At some point, you will want to intansiate the object, ie:
Code:
m = new MyClass();
 
Thanks oppcos, but I am looking for the answer about my "why" :)
 
Well, a class is more of a type of something than the something itself (an object). You can define an int Xyz and use it right away, but you have to allocate memory and create a new instance of an object. As such, you use the new operator with a pointer of type MyClass to create an actual instance of the methods and properties. Those properties and methods are now independent of any other MyClass objects you create.

The * operator indicates that what you are getting is a pointer. The & operator indicates you are actually getting the address indicated by the variable and that that address is pointing to object whose type you've specified. *&, I believe, is indicating that you are getting a pointer to a pointer of an object, and seems a little confusing.

What you are likely trying to do is modify the object's memory directly, passing by reference instead of passing by value. Since you create an object typically by using a pointer to its type with the new operator (instead of creating the memory for it yourself), you already have a pointer you can pass to your function to reference the memory by reference.

I may not be explaining well, or what you are after. Please clarify your question so I don't just go of rambling.
 
OK, let me clarify the question.

We can see the only difference is:

1:
f(new MyClass);

and

2:
MyClass m;
f(&m);


So, the question is:
Why "void f(MyClass *& mc)" is compilable with "1", ,but cannot with "2"?

Actually I compiled code 1 with AIX's xlC and Solaris' CC, xlC got an error, but CC got succeeded. So I guess that "*&" in "void f(MyClass *& mc)" is not a standard prototype. So, if possible, anyone could give a proof if "*&" makes any sense or has any benefit?
 
I can't give any "proof," but I can tell you that a reference to a pointer is perfectly fine and legal. It's useful for the same reason any reference parameter is useful: the function can modify the original parameter, and the caller doesn't need to make his code look messy by passing a pointer to a pointer.


My guess is that [tt]f(&m)[/tt] doesn't work just because there's no variable to take the reference of. The return value from [tt]new[/tt], on the other hand, is getting treated as a temporary anonymous variable that only exists as long as there's a reference to it.

Does it work better if you try this?
Code:
MyClass m;
MyClass *mp = &m;
f(mp);


Actually, for me, neither #1 nor #2 compile, and my above suggestion does. I'm using GCC 3.2.

Therefore, it may just be a bug in your compiler. Maybe it shouldn't be compiling either of them. That is, perhaps the result of a function or operator call ([tt]new[/tt]) shouldn't be a temporary variable, and since there aren't any variables involved to take the reference of, neither example is legal.

You'd have to get someone to comb through the C++ Standard to figure out what behavior is correct, and I'm not volunteering.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top