Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

pointing to a private member

This topic is 4139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

suppose i have the following class class foo{ private: int var; public: int GetVar(){return var;} void SetVar(int v){var = v;} }; then i make a pointer by doing foo SomeClass; SomeClass.SetVar(1); int *p_var = SomeClass.GetVar(); *p_var = 5; //program crashes why does the program crash? how can it even compile? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
return var returns the value of var - not var itself. When you do int *p_var = SomeClass.GetVar(); you're creating a pointer which points to whatever the value of var is. That is, your pointer is invalid.

So, when you dereference the pointer and try to write to it, you will hit an error because you're trying to access memory that isn't yours.

It will compile because you it's perfectly valid code, syntactically. However, you will (almost) never need to do it (and indeed, shouldn't).

Why do you want a pointer to a private member anyway? The point of making it private is that it's part of the implementation rather than the interface and you don't need access to it. What you're trying to do is wrong, never mind the fact that it will cause an error.

Share this post


Link to post
Share on other sites

... In Class ...
int* GetVarPointer() { return &var; }

... In Main ...
int* lol = SomeClass.GetVarPointer();
*lol = 5;


Should work?

Share this post


Link to post
Share on other sites
oh, i meant:
int *p_var = &SomeClass.GetVar(); //the '&' was added
i'm not actually using this in code, it was just something i was wondering about, why doesn't p_var point to var?
Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by daniel_i_l
oh, i meant:
int *p_var = &SomeClass.GetVar(); //the '&' was added
i'm not actually using this in code, it was just something i was wondering about, why doesn't p_var point to var?
Thanks.


That shouldn't compile... The return value of GetVar isn't an l-value. The &SomeClass.GetVar() should throw an error. The return value will be passed in a register (probably EAX) - and so has no address to take.

EDIT: Definitely doesn't compile in VC++EE - what compiler are you using?

Share this post


Link to post
Share on other sites
Quote:
Original post by daniel_i_l
oh, i meant:
int *p_var = &SomeClass.GetVar(); //the '&' was added
i'm not actually using this in code, it was just something i was wondering about, why doesn't p_var point to var?
Thanks.


[edit: if it were to compile...]

GetVar() returns a copy integer on the stack. You use operator & to obtain its address. As it is a temporary its scope ends now and you have an invalid pointer. If you dereference that pointer you will get "undefined behaviour", it may or may not crash, it may even appear to work ( reading rather than writing may appear to work ). To get the actual pointer you need to use Kobalt64's code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kobalt64

... In Class ...
int* GetVarPointer() { return &var; }

... In Main ...
int* lol = SomeClass.GetVarPointer();
*lol = 5;


Should work?


Yes. But that's definitely Not A Good Thing. A private variable is private for some reason. Doing this (or the reference version) simply destroy this needed encapsulation, not to mention that it allow you to do some weird and dangerous things (trying to "delete lol;" will result in a crash, as well as forgetting to init it).

So, to answer daniel_i_l: ultimately, the program will "crash" because your computer wants to say you that you did something which is dangerous, error prone, unneeded and in total contradiction with all the known object-oriented software design laws and principles [grin].

In particular (and this is, from my point of view, a correlary of the Law of Demeter (this may be a good to exercise your google-fu madskillz on this name)), whatever you want to do with a private member M of a class C is better done with a member function of this class C. The reason is that if an operation OP on M makes sense (that is, if OP(C.M) makes sense) then implementing OP in a member function of C also make sense - and is far better. Now, if implementing OP as a member function of C1 doesn't make sense, then it means that having M as a member of C doesn't make sense either.

So: whenever you have
OP(C.M)
transform it to
C.op() { OP(M) }
Unless it doesn't make sense to implement it in this way, in which case you should reconsider why M is a member of C in the first place.

Regards,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!