Y
Yves Dhondt
Hello,
Currently I have the following "design":
class A { ... };
class B : public A { ... };
class C : public A { ... };
class Z {
private:
A *a;
public:
Z() { a = new A(); };
~Z() { delete a; };
A *GetObject() { return a; };
};
int main() {
Z *z = new Z();
A *temp = z->GetObject();
delete z;
return 0;
}
Now I want Z being able to use B or C in stead of A. So when creating Z
I should somehow be able to specify what it should use (A, B or C).
The easiest way that comes to mind is using templates to specify what
class type I want to use in my instance of Z.
template<class T> Z {
private:
T *t;
public:
Z() { t = new T(); };
~Z() { delete t; };
T *GetObject() { return t; }
};
int main() {
Z<C> *z = new Z<C>();
C *temp = z->GetObject();
delete z;
return 0;
}
But I would like to avoid templates (plans for porting the code to
embedded). Is there a better/other way of doing this?
TIA
Yves
Currently I have the following "design":
class A { ... };
class B : public A { ... };
class C : public A { ... };
class Z {
private:
A *a;
public:
Z() { a = new A(); };
~Z() { delete a; };
A *GetObject() { return a; };
};
int main() {
Z *z = new Z();
A *temp = z->GetObject();
delete z;
return 0;
}
Now I want Z being able to use B or C in stead of A. So when creating Z
I should somehow be able to specify what it should use (A, B or C).
The easiest way that comes to mind is using templates to specify what
class type I want to use in my instance of Z.
template<class T> Z {
private:
T *t;
public:
Z() { t = new T(); };
~Z() { delete t; };
T *GetObject() { return t; }
};
int main() {
Z<C> *z = new Z<C>();
C *temp = z->GetObject();
delete z;
return 0;
}
But I would like to avoid templates (plans for porting the code to
embedded). Is there a better/other way of doing this?
TIA
Yves