Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Yanroy

Pointers to member variables

This topic is 6930 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

Is it legal to have a function return a pointer to a private/protected member? Ex: class Test { private: int PrivateInt; public: int * GetPrivateInt() {return &PrivateInt;} }; Test Test1; int *TempInt = Test1.GetPrivateInt(); // use TempInt (even change it and have the class use the changed information) This would greatly simplify the player stats/classes/races in my MUD if I could pass stat/class/race structs into the class, then get them back out again later. --------------------

You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

Share this post


Link to post
Share on other sites
Advertisement
Legal? Yes. Sensible? No There's virtually no point in having things as private if you're allowing functions to take their own personal reference to them to be able to change them at any time. Why not just make it public? An almost-certainly better way is to just have a SetMemberVariable() function which you use to set the variable. You may lose a tiny bit of efficiency through copying things, but you gain in encapsulation. An alternative is to declare a certain class or function as a friend of the class (in this case Test) so that it alone (along with any other friends) can have direct access without needing an all-purpose encapsulation-breaking function

There is one exception: it's common practice to return const pointers (or more commonly, a const reference) to a data member so that it can be read but not altered.

Edited by - Kylotan on 5/2/00 10:30:08 AM

Share this post


Link to post
Share on other sites
How do I declare a class/struct as a friend of another class/struct? And can I make classes frends of structs? I have read about the friend keyword, but I don''t know the syntax.

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

Share this post


Link to post
Share on other sites
To make a class or function a friend of another class, list the friends of the class in the class definition:

class A
{
friend class B;
friend void foo(const A &a);
...
};

class B
{
...
};

void foo(const A &a)
{
...
}

B can access protected and private members of A. The same holds for foo.

Since classes and structs are the same, you can make a struct a friend of a class and vice versa.

Erik

Share this post


Link to post
Share on other sites
There are situations where you want to "expose" private members (that doesn''t sound to good does it?) .

In my DDraw engine, all the Surface pointers are private. But, advanced users may want to do something to the data that I hadn''t thought of. So they can do a:
myDDraw->GetPointer(&... blah blah);

And now they can mess directly with the picture data. Newbies are still protected, and experts get what they want. Everyone is happy!

Share this post


Link to post
Share on other sites
quote:
Original post by Buster

In my DDraw engine, all the Surface pointers are private. But, advanced users may want to do something to the data that I hadn''t thought of. So they can do a:
myDDraw->GetPointer(&... blah blah);


I have made some classes which return a pointer. For instance, an *deep breath* LPDIRECTDRAWSURFACE4. But by returning the pointer, they can modify the stuff it points at but not break the pointer (ie. change it.)

And it''s all very well thinking that experts can use the direct access, and newbies will not. But more often than not, newbies -think- they know more than they do And would perhaps end up bypassing your neat API cos they think they know a better way of doing something, when usually they don''t

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
new, so I don''t know how to do the quote stuff, but about the newbies can''t screw it up but experts get what they want:
Declare your private members as protected, so if someone wants to change the implementation, they can just derive their own class from yours. (Of course, depending on the purpose of the class, you may have to use virtual functions, but the overhead is not that great when used sensibly.)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!