Jump to content
  • Advertisement
Sign in to follow this  
masterbubu

Need help

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

Hi, The scenario: //...................... Class A{ protected: int num; }; //...................... #include "A.h" Class B : private A { }; class A implements some API, and class B is a wrapper that simplify the use of class A. I what to give the user an option to create only class B. Now is there a way to hide class A from the user ? (the user won't recognize class A, and won't be able to inherit from it). [ Class B must inherit from class A, friend releation is not an option, because Class A should not recognize class B. ] tnx,

Share this post


Link to post
Share on other sites
Advertisement
PIMPL.
// b.hpp
class A; // forward declaration

class B {
private:
A * pA;
};


Inheritance is not needed. If user doesn't have access to A, then the fact that B is A doesn't matter.

PIMPL design above hides the implementation, so if A can be provided by a static library without code, or something similar. Actual instances of A are then created inside .cpp, for which the source doesn't need to be provided. a.hpp does not need to be included to use b.hpp.

IMHO, there is little point in hiding code. All of this "must not extend" is not worth it, except in enterprise environments, and even then it's being abandoned since it causes more hassles than it saves.

Share this post


Link to post
Share on other sites
You can prevent class A from being inherited from by making it's constructors private. It should designate class B as a friend so B and only B can inherit from it.

Share this post


Link to post
Share on other sites
Well the solutions only answers on only one part of the question, but still there are problems:

Antheus, indeed you hide A but, when you want to replace B, you have to implement A all over again.

taz0010, the same problem, if you what to add D class, you need to add to A code another friend relation.
( A code should be close, and don't care about others class ).

Is there a ways of doing it at all?

I can of think on ugly way ( throw constructor exception but hell it not nice ).

Share this post


Link to post
Share on other sites
Quote:
taz0010, the same problem, if you what to add D class, you need to add to A code another friend relation.
( A code should be close, and don't care about others class ).


You're trying to design a class that can only be inherited from by certain other class(es). By definition, this means your base class needs to know about the derived classes which are allowed to inherit from it. A base class which is independent of it's children does not care what inherits from it.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!