inheritance without using dynamic_cast

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,

 
S

Stuart Redmann

aaragon said:
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;
};

Your base class AbstractClassA is pretty useless this way. Unless _this_ class
contains some pure virtual method declarations, there is no point in using it.
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

Right. You can only call methods declared in AbstractClassA.
You are mixing two design concepts of C++ that almost are mutual exclusive.
Virtual inheritance is used to device at run-time which functionality should be
used. In contrast to this, templates are used when you know at compile-time
which functionality you want but don't want to code a similiar algorithm for
various data types (this is the most probable scenario for using templates, of
course there are cases where templates depend on other parameters than types).
You cannot mix these two concepts!

In your case it looks as if the class that should contain the virtual methods
should be the class that you pass as template parameter B to the template
classA. We can only give you a proper assessment of your design if you tell us
what you are trying to do (unless this is confidental :)

Regards,
Stuart
 
A

alan

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;
virtual sometypethatdoesntneedtemplates
someFun1(sometypethatdoesntneedtemplates&);
};

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
You can't get these from an AbstractClassA pointer:
 
S

shazled

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 had a similar problem but only because I wanted to put the different
templated classes in same datastructure.
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 my particular problem, I found the visitor design pattern helped.

Saul
 
A

aaragon

I had a similar problem but only because I wanted to put the different
templated classes in same datastructure.





For my particular problem, I found the visitor design pattern helped.

Saul

That is exactly what I'm using now. I can use an abstract base class
for the common part of the classes, and then use a visitor to work
with specific functions of the derived template clases. It is actually
very cool stuff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top