Jump to content
  • Advertisement
Sign in to follow this  
Supernat02

Access to Class Private Members

This topic is 3737 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 am writing a container class that just holds a list of pointers to another class. Each of the sub classes is uniquely identified by a provided "Name" in string format and a filename. The Name portion can be empty or unique, no other choices. If the same filename is requested to be loaded, the container class will increment a reference count in the sub class and just return the index of the sub class. That's the basic form. My question is this. What's the best approach for doing the Reference count in the sub class? I would like to do it so that the user can not call the Reference() method but so that it can be called only by the container class. Is there a syntax of the "friend" keyword that will let class A::member A be accessible to class B but not anything else from Class A? Or some other syntax? Thanks, Chris

Share this post


Link to post
Share on other sites
Advertisement
Friend will totally subvert the class encapsulation. So

class Foo
{
private:
void Method();
friend class Bar;
}

means class Bar can call Foo::Method even though it's private (or protected when Bar is not a Foo).

Now in general from a "good clean code" point of view, friend is one of those last resorts you really want to avoid. But for very small contained set of collaborating objects (like a smart pointer and a reference counted base class) they can be quite useful. They're often used to make it impossible/very hard to circumvent reference counting on the object.

Just google "smart pointer" and you'll find a bunch of implementations - including a pretty standard one in boost.

Share this post


Link to post
Share on other sites
Alpha_ProgDes, this is C++. However, I'm not deriving the container class from the instanced class. The container will just hold pointers to the instance.

Milkshake, I definitely don't want to allow access to all private members, just 2 functions (reference and dereference), so I take it this is not possible. I haven't really found anywhere where you could do this, so I'm not surprised. I will research the smart pointers! Thanks!!

Share this post


Link to post
Share on other sites
not sure I completely understand the scope of your problem, but I don't think smart pointers are really required for this. There are other methods for inheritance that may be worth investigating, its possible to inherit from a base class in many different ways, which have different sets of behaviour rules.

i.e.

class base
{
};

class child : private base
{
};

and also, (I think)

class child2 : protected base
{
};

have a look at http://www.cprogramming.com/tutorial/private.html

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!