A
aaragon
Hello all,
I think the problem I'm facing is hard so I wonder if someone faced it
before and if there is a simple solution to it. I am creating a
program that basically takes all the required information at runtime
after reading a file. After reading the file, several objects of a
templated ClassA are created. I thought that the way to do this is to
create an abstract class AbstractClassA and have all templated classes
inherit from the Abstract class. Doing this I can work with pointers
to the base class. In code:
AbstractClassA {
...
...
virtual ~AbstracClassA = 0;
};
template<class B, class ATraits>
class classA : puclic AbstractClassA, public ATraits {
// overide base class member functions
...
// add other functions specific to the class
...
// some functions that depend on the template parameter
B someFun1(B&) {
...
}
B someFun2() {
...
}
};
Now, the problem is that I cannot call an additional function of
classA that is not in the interface of the abstract class because the
compiler complains (as it should). For example, I cannot call
typedef classA<int,IntTraits> caint;
AbstractClassA* pa = new caint();
int i1 = 10;
pa->someFun1(i1); // error, someFun1 is not defined in AbstractClassA
Of course, I cannot declare this function in the abstract class,
because the function depends on a template parameter that doesn't even
exist at that point.
One dirty solution to this is to depend on the RTTI and use
dynamic_cast against every single instantiation of ClassA. However,
imagine that I have 100 of these different instances! That's why I
templetized the class in the first place, I don't want to do any
dynamic_cast on it.
So I guess my question is if my logic right or I'm missing something
here? if it is right, is my design right? or there is a better
approach to do what I want to do?
Thank you all,
a²
I think the problem I'm facing is hard so I wonder if someone faced it
before and if there is a simple solution to it. I am creating a
program that basically takes all the required information at runtime
after reading a file. After reading the file, several objects of a
templated ClassA are created. I thought that the way to do this is to
create an abstract class AbstractClassA and have all templated classes
inherit from the Abstract class. Doing this I can work with pointers
to the base class. In code:
AbstractClassA {
...
...
virtual ~AbstracClassA = 0;
};
template<class B, class ATraits>
class classA : puclic AbstractClassA, public ATraits {
// overide base class member functions
...
// add other functions specific to the class
...
// some functions that depend on the template parameter
B someFun1(B&) {
...
}
B someFun2() {
...
}
};
Now, the problem is that I cannot call an additional function of
classA that is not in the interface of the abstract class because the
compiler complains (as it should). For example, I cannot call
typedef classA<int,IntTraits> caint;
AbstractClassA* pa = new caint();
int i1 = 10;
pa->someFun1(i1); // error, someFun1 is not defined in AbstractClassA
Of course, I cannot declare this function in the abstract class,
because the function depends on a template parameter that doesn't even
exist at that point.
One dirty solution to this is to depend on the RTTI and use
dynamic_cast against every single instantiation of ClassA. However,
imagine that I have 100 of these different instances! That's why I
templetized the class in the first place, I don't want to do any
dynamic_cast on it.
So I guess my question is if my logic right or I'm missing something
here? if it is right, is my design right? or there is a better
approach to do what I want to do?
Thank you all,
a²