Can you return a class member object as a pointer?

Started by
18 comments, last by iMalc 11 years, 3 months ago

This may sound stupid (I'm pretty sure you can't) but I'm wondering if I have:


class a {}

class b
{
public: 
    
    a GetA(){return MyObject;}

private:

    a MyObject;
};

Instead of GetA() returning MyObject as an object is it possible to return it as a pointer(*)?

Thanks

Advertisement

Yes.

As a pointer:

a* GetA() {return &MyObject;}

MyB.GetA()->DoSomethingToMyObject();

or as a reference:

a& GetA(){return MyObject;}

MyB.GetA().DoSomethingToMyObject();

Returning a pointer or a reference to internal class data is of course valid, but one has to be careful not to use the pointer or reference if the object is destroyed.

a *my_a_pointer; { b my_b; my_a_pointer = b.GetA(); } my_a_pointer->DoSomethingToMyObject(); // <-- Trouble! my_b has been destroyed already

This is not too different from what happens when you obtain a char const * from a string using std::c_str(). But it's still worth mentioning.

What does pointer contain as a VALUE? An address. Therefore - just return the address.

Can you return a class member object as a pointer?

Did you ask the compiler?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

If you want to return a reference to the class member, I suppose the reason is that you want to modify the member. But if the reason is that the object is big, and you want to improve performance by not having to return a copy, it may be a good idea to return a const pointer to the object, or const reference.

Returning a pointer (const or not) is valid C++, but it violates some principles of object oriented programming. If you want the object updated, the usual principle to have the update logic in a member function instead.

To expose internal design of a class to the outside is usually a bad idea. That means you have an extra dependency, and dependencies should be kept to a minimum. Suppose you later find out that the data need to be managed in another way, you can no longer update only the class 'b'. You will also have to find all users of GetA(), and update them. If it is a library, it may not be possible, and the API is suddenly incompatible.

[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Maybe I'm nitpicky, but isn't it a bit wrong (and slightly confusing) to say that you "return an object as a pointer"?

Too me, that doesn't really make sense.
What you do is "returning a pointer that points to the object".

The pointer itself is an object too, a very different object then the class object it (maybe) points to.
I think that remembering this makes the answer more obvious.

But again, I'm not even a native english speaker, so maybe I read too much into the grammar....
Maybe I'm nitpicky, but isn't it a bit wrong (and slightly confusing) to say that you "return an object as a pointer"?

Too me, that doesn't really make sense.
What you do is "returning a pointer that points to the object".

The pointer itself is an object too, a very different object then the class object it (maybe) points to.
I think that remembering this makes the answer more obvious.

This is an excellent point. And, while OT, another important one: people will talk about pass-by-value/pass-by-reference. in C/C++, everything is pass-by-value; it's just that sometimes, you pass values that are pointers, so the objects they represent don't copy.

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

in C/C++, everything is pass-by-value...

[/quote]

Please identify the value is being passed in the following code:

void frobnicate(int &example) { example = 42; }

in C/C++, everything is pass-by-value...

Please identify the value is being passed in the following code:


void frobnicate(int &example) {
    example = 42;
}

I suppose he would've been correct if he said, "In C, everything is passed by value...."

But, not so for C++, as you point out.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement