Archived

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

C++ selectively hiding a class

This topic is 5379 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'm trying to figure out the best way to organize some classes in my game. Let's say I have two classes, CPublic and CHidden: class CPublic { ... private: CHidden * m_Hidden; }; class CHidden { ... private: // hide the constructor CHidden(); // only let CPublic have access to the constructor friend class CPublic; }; As implied above, I only want CPublic to be able to create instances of CHidden, but I still want other classes to be able to use instances. Is this the best way to go about doing this? I don't particularly like it because the "friend" notification also allows CPublic to have access to CHidden's private data members... Thanks! [edited by - tls284 on March 26, 2003 12:34:48 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by tls284
I don''t particularly like it because the "friend" notification also allows CPublic to have access to CHidden''s private data members...

Thanks!



So what''s the problem with that? While it is usually considered bad practice if done too often, don''t let some narrow-minded OOP theory get in the way of you solving your problem, which is all that matter in the end anyways, right?

Share this post


Link to post
Share on other sites

  
class CHidden
{
public:
CHidden(int x = 0);
};

//in cpp, hidden from the cruel world:


CHidden::CHidden(int x) {
assert(x == 34689276);
}

And then you can only instantiate it only by knowing the secret id.

Just kidding. I think Dobbs is correct. Don''t try building too many walls for yourself. Put in class documentation: "Clients shouldn''t instantiate this" and leave it at that.

Share this post


Link to post
Share on other sites
Don''t worry about it, I think everyone goes through an over-engineered code phase

I can''t remember my C++ class/scoping rules... can you work with instances of a nested class outside its containing class? I think Java can do something like that.

Share this post


Link to post
Share on other sites

  
class Public
{

private: // or protected:

class Hidden
{
// ...

};

// ...


};



EDIT: wrote code instead of source for the tag name :-( I do that a lot!

-SniperBoB-


What if The Matrix was recursive?

[edited by - snprbob86 on March 26, 2003 2:58:18 PM]

Share this post


Link to post
Share on other sites
An obvious problem with that code is he wants outside classes to be able to use instances of the nested class. Since the nested class is private they can''t. I don''t have a C++ compiler in front of me, so can someone tell me if public nested classes can be seen by other classes? All the info I''ve found about nested classes on Google seems to relate to how broken and buggy various compilers are in supporting them.

Share this post


Link to post
Share on other sites
I did something like this when I made a factory-pattern-style construct. I had a "manager class" with a method CreateEffect that took a string with the effect type, which created an object with the appropriate type. Each effect type''s constructor was private, with the manager class set as friend.

This keeps all the object creation logic in one place. Note that this is my personal interpretation of the factory pattern, I didn''t read the book.

Share this post


Link to post
Share on other sites
Grul - yeah that''s pretty much what I''m doing actually.

I have a sprite factory for my 2D game, which handles dynamically loading/unloading sprites as they''re needed as well as ensuring that there is only a single copy of the surface loaded into memory for all objects referencing it.

Thus the factory is making Sprites for calling objects, but I don''t want calling objects to be able to create sprites themselves.

Anyway, I''m rambling, thanks for the help everyone.

Share this post


Link to post
Share on other sites