Dynamic_cast from private derived to public base
-
I searched details about inheritance and dynamic_cast but I haven't figured out if this is possible and, if it is, any side-effects.
I have a base class which has some public attributes. The base class is not a "proper" class, it's actually a struct, which has all its members public by default.
@struct base{
// public:
int a;
float b;// initializing through constructor
base(){
a=0;
b=0.0;
}
}@Now, when I create a derived class I want the base class attributes not to be accessed directly because I have to make validity checks and other things whenever they should be modified. For this purpose I have public methods which would do the work, and I derive the base class as private.
@class DerivedClass : private base{
public:
setVarA(int value){ a = (value>0)?value:0; }
setVarB(float value){ b = (value<5.0)?value:0; }//remember that a and b are now private
/**************
private:
int a;
float b;
***************/
}@I need base to have its members public because it contains the basic structure of an object, and it's by far more convenient to use such a simple struct when passing it as function parameters (I often need to) instead of passing a whole class with all methods and any other members.
The final questions are:
@DerivedClass * MyDerivedClass = new DerivedClass;
base * casted_class = dynamic_cast<base *>(MyDerivedClass); // is it allowed ?@if I perform a dynamic_cast on the derived class to the base class, will it work just fine even if the derived class has private members instead of public?
If this is allowed to do, does that mean that I can cast any derived class to one of its base classes regardless of the inheritance access type? (f.e. casting protected to private or to public)
Are there any side-effects ? -
Hi,
at first i have to say that your assumption about passing objects as function parameters is correct but there exists a workaround for the overhead:
Pass the object by reference:
@void function(base &anObject)@
if the object should not be changed in function pass it as const reference:
@void function(const base &anObject)@
At second i have to say since it breaks encapsulation it is always a bad design to have public class members unless you have a real good reason to do so.
When i understand you correctly your base class only function is to provide a base for derived classes. If only the derivates shall have access to base's members just declare them as "protected".
In this case it is also a good idea to declare the constructor of your base class as "protected" so it could never be the case that an object of type base comes to life accidentally.
Here is some information about type casting in C++:
http://www.cplusplus.com/doc/tutorial/typecasting/
Here is a link to some more information about private inheritance:
-
Thank you very much for replying ;)
I have figured it all on my own since then, but I didn't think about using references which could have saved me some work. Also, in the meantime the whole application design has changed and I totally left out this base-derived approach.