Re: Derived Structure in Derived Class??
David wrote:
Is it possible to have a structure in a base class
be expanded in a derived class using the same memory
area.
In most cases, when you treat C++ classes from desing point of view,
this is not a question - "to share the same data by other names",
because you refer to interfaces, without it, it is not easy to see why
do you want to change names.
Memory area is shared by the same types means the same members. To
change names you need user-defined functions, like this:
struct tagStructB : sStructA
{
int& d(){ return a; }
const int& d()const{ return a; }
int& e(){ return b; }
const int& e()const{ return b; }
int& f(){ return c; }
const int& f()const{ return c; }
};
To hide differences between access to "a" and to "d" you need user
defined functions of sStructA also
struct sStructA
{
int& a(){ return _a; }
const int& a()const{ return _a; }
int& b(){ return _b; }
const int& b()const{ return _b; }
int& c(){ return _c; }
const int& c()const{ return _c; }
private:
int _a;
int _b;
int _c;
};
There is proposal for C++ improvement, where compile time references
can me member of class, the references can be used without overhead to
make access to data-members, i do not remember exactly, but something
like this
struct tagStructB : sStructA
{
//they take no memory,
//but must refer to appropriate class members only
int& d=a;
int& e=b;
int& f=c;
};
the renaming is a kind of syntax sugar, can help you to remove extra
standard functions.
To change type of members you need union to be used, but instead of
inheritance you need composition to be used
union tagStructB
{
//sStructA
int a,b,c;
//tagStructB
char d,e,f;
};
To hide improvements of sStructA you can use tepmlate<>
template<class data=sStructA>
class CMyClass
{
data m_StructData;
...
};
template<class data=tagStructB>
class CMyDerivedClass : public CMyClass<data>
{
data m_StructData;
...
};
and turn them into plain code
typedef CMyClass<tagStructB> MyClass;
typedef CMyDerivedClass<tagStructB> MyDerivedClass;
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new