C++ selectively hiding a class

Started by
8 comments, last by tls284 21 years, 1 month ago
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]
Advertisement
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?
Why do you want a solution in code? If you don''t want other code to instantiate CHidden.... just don''t instantiate it.

  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.
Haha, fair enough...thanks for the reality check
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.

  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]
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.
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.
"Archangel, dark angellend me thy lightthrough death''s fail until we have heaven in sight"
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.

This topic is closed to new replies.

Advertisement