Jump to content
  • Advertisement

Archived

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

BradDaBug

Keeping my privates private

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

I''ve got this class, and its got some private parts. This class is declared and all in this header file. Everything that needs to use the class gets this header fine included. The problem is that this class needs some funky data storage types to store the stuff it needs to work with. If I go and declare whatever it needs in the header, then EVERYTHING that uses that class will have to also know about those funky data types, which totally defeates the purpose of having private members, right? How can I get this class to have all the private stuff it needs without the stuff that uses the class having to know about all the funky data types? Doesn''t DirectX do something like this? Don''t we have access to all the funtions and stuff, but have no idea what''s running underneath? I don''t think I''ve ever seen "This is a private member" in the DirectX docs.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
for distribution purposes, you would have a header file which only listed the public and protected members. since you would only be distributing a compiled library the private members would not be needed to be listed.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
for distribution purposes, you would have a header file which only listed the public and protected members. since you would only be distributing a compiled library the private members would not be needed to be listed.


Interesting. Does that actually work? I''ve never tried it, but I don''t see why it wouldn''t.

Share this post


Link to post
Share on other sites

  
class MyClass
{
public:
void DoSomething();
private:
class ComplexHiddenClass // now other code is not exposed to this datatype

{
int i, j;
}
}




Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
BradDaBug, people knowing what data types you use doesn''t "totally defeat" the purpose of having private members. People still can''t change them, they''ll only know what they are. But if you really need to protect clients from seeing any implementation details, look up for pimpl idiom.
quote:
Original post by daerid
Interesting. Does that actually work? I''ve never tried it, but I don''t see why it wouldn''t.
Well, the compiler wouldn''t know the size of the class if it didn''t know about it''s private members.

Share this post


Link to post
Share on other sites
You can use the pimpl idiom (google)


  
class dogImpl;

class dog : public animal {
private:
dogImpl* pImpl;
public:
dog();
void speak(const std::string&);
}


Or you can have an abstract base class which is the only thing exposed. So you''d only show animal and people could only hold pointers to the objects.


  
class animal {
virtual void speak(const std::string&) = 0;
virtual ~animal() {};
}

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
for distribution purposes, you would have a header file which only listed the public and protected members. since you would only be distributing a compiled library the private members would not be needed to be listed.

That would certainly defy the ODR (One-Definition-Rule). Do it like petewood suggested: Use pimpl[^] or an abstract base class.



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''

Share this post


Link to post
Share on other sites
Its not really other people i''m worried about. I just don''t like other .cpp files that use this class to actually have to know about all the stuff that class uses.

Like this:
class Toilet
{
private:
Water *water; // Water is a class
Poop *poop; // Poop is a class
};

Everything that uses the toilet is also gonna have to know about water and poop. That bugs me. It seems like that since they''re private and the stuff that''s using toilet shouldn''t have to know ANYTHING about them.

I''m now experimenting with something like this:

class toilet
{
#ifdef INSIDE_TOILET_CPP
Water *water;
Poop *poop;
#else
void *water;
void *poop;
#endif
};

And of course INSIDE_TOILET_CPP would be declared withen toilet.cpp, where the actual toilet class is fleshed out, and no where else. So would this work ok?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!