Pointers to member variables

Started by
5 comments, last by Yanroy 23 years, 11 months ago
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.

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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
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.

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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
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!
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
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.)

This topic is closed to new replies.

Advertisement