I understand the mechanism of downcast, that is, base class can be cast into the sub class when the sub class object is instantiated in the base class pointer.
However, sometimes, it is needed that initially base class is constructed (and generate some member variables common to the sub classes), and needed to be cast (or copied) to the sub class. Of course, such a cast is usually prohibited in the way of downcast (dynamic_cast etc).
Is there a way to treat such a case in the scheme of C++ language specification?
(I come up with the idea to handle such a situation as virtual function to copy base class in the sub classes as the below. But, I feel this is bothersome when the base class has a lot of member variables, which needs many copy (substitution) statements. Is there a smart way?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Base{
protected:
int _dummy_base;
public:
virtual Base* copyFromBase(Base* obj) {returnnew Base(*obj);};
};
class SubClass : public Base{
protected:
int _dummy_sub;
public:
virtual Base* copyFromBase(Base* obj) {
SubClass* ret = new SubClass();
ret->_dummy_sub = 0;
ret->_dummy_sub = obj->_dummy_base;
return ret;
}
};
> it is needed that initially base class is constructed
> and needed to be cast (or copied) to the sub class.
then you surely have a constructor like this
1 2 3 4
SubClass::SubClass(const Base &b):
Base(b)
//¿how to initialise subclass members?
{}
or you may call the assignment operator
1 2 3
SubClass* ret = new SubClass();
this->Base::operator=(*obj);
//¿what about subclass members?
now, ¿what values will have the subclass member variables?
this smell of bad design
>this smell of bad design
I remembe it was written that such a procedure was bad design in a book (effective c++?)
My code's intention is
(1) generate Base class object in a lot of different manner which contains many common member variables to derives.
(2) Then, cast the into derives, and set individual member variables for derives
>As far as I know, a derived class contains all the base class properties… Doesn’t it?
>Can’t you access them because they are private?
All public. But, constructor of Base class is designed to set all properties as arguments. Thus, before (1), I cannot construct derives and pass it to the generation step(1).
SubClass::SubClass(const Base &b):
Base(b)
//¿how to initialise subclass members?
{}
Is this copy constructor permitted?
member variables of subclass is just public member and no pointer, simple initialization.
I feel this statement works well.
> My code's intention is
> (1) generate Base class object in a lot of different manner which contains
> many common member variables to derives.
> (2) Then, cast the into derives, and set individual member variables for derives
not sure if follow
you have one object of class base, then you want to operate on that object in different ways with objects of derived classes
sounds like `strategy' or `decorator' pattern
¿why all these object of derived classes have the same state?
perhaps you may simply point to that other object
¿is really inheritance the relationship between the classes?
nolyc wrote:
You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless you describe what X is.
> SubClass::SubClass(const Base &b)
be careful
1 2 3
Base obj;
Subclass1 a(obj); //this works
Subclass2 b(a); //this also "works"
struct Bar {
Bar( const Bar & ); // copy constructor
};
struct Derived : Bar {
Derived( const Bar & b ) // not a copy constructor
: Bar( b ), // initialize base class with data. Legal
x( 42 ) // initialize member with data
{}
Derived( const Derived & rhs ) // copy constructor
: Bar( rhs ), // initialize base class with data. Legal
x( rhs.x ) // initialize member with data
{}
private:
int x;
};
struct Foo {
Foo( const Bar & b )
: gaz( b ) // initialize member with data. Legal
{}
private:
Bar gaz;
};
>¿why all these object of derived classes have the same state?
>perhaps you may simply point to that other object
>¿is really inheritance the relationship between the classes?
Is it anti-pattern to have the same state in all inheritance family?
Common state is in Base class.
BTW, this problem necessarily rooted in the design of generation of objects.
I know there are some design patterns (Abstract Factory, Builder etc).
But, I could not grasp their usefulness for my poor experience.
If you are familiar with the case of their best practices,
could you tell me?