Templates and virtual base class

P

Peter Hagenaar

Hello there.

I have a question about a virtual template class.
The class wich derives from this class overrides a virtual method in
this template base class and calls the same method in this base class.
Unfortunately, the baseclass method gets called but the preceding code
in the derived class isn't. Mind you you that the base class is a
template class.

Does anybody of you have an idea why this isn't working?

Thanks in advance. (There is a code example end the end of this message)

- Peter Hagenaar

Code example:

I've created a virtual template class of the form:

template<class T>
class BaseClass
{
public:
virtual void DoStuff(void)
{
MessageBox("C++ BaseClass");
};

T MyVar;

};


and a derived class of the form:

class DerivedClass : public BaseClass<MyType>
{
public:
virtual void DoStuff(void)
{
BaseClass<MyType>::DoStuff(); // this one is called

MessageBox("C++ DerivedClass"); // not executed !

int dummy = 0; // not executed !

dummy += 23456; // not executed !
};

};
 
J

John Harrison

Peter Hagenaar said:
Hello there.

I have a question about a virtual template class.
The class wich derives from this class overrides a virtual method in
this template base class and calls the same method in this base class.
Unfortunately, the baseclass method gets called but the preceding code
in the derived class isn't. Mind you you that the base class is a
template class.

Does anybody of you have an idea why this isn't working?

Thanks in advance. (There is a code example end the end of this message)

- Peter Hagenaar

Code example:

I've created a virtual template class of the form:

template<class T>
class BaseClass
{
public:
virtual void DoStuff(void)
{
MessageBox("C++ BaseClass");
};

T MyVar;

};


and a derived class of the form:

class DerivedClass : public BaseClass<MyType>
{
public:
virtual void DoStuff(void)
{
BaseClass<MyType>::DoStuff(); // this one is called

MessageBox("C++ DerivedClass"); // not executed !

int dummy = 0; // not executed !

dummy += 23456; // not executed !
};

};

Either something is wrong with your compiler or something is wrong with your
example. Try this one

#include <stdio.h>
struct MyType {};

template <class T>
class BaseClass
{
public:
virtual void DoStuff(void)
{
printf("C++ BaseClass\n");
};
T MyVar;
};

class DerivedClass : public BaseClass<MyType>
{
public:
virtual void DoStuff(void)
{
BaseClass<MyType>::DoStuff();
printf("C++ DerivedClass\n");
};
};

int main()
{
BaseClass<MyType>* x = new DerivedClass();
x->DoStuff();
}

Prints

C++ BaseClass
C++ DerivedClass

as expected.

Maybe you don't really have a derived class object? Maybe you've sliced the
derived part or made some other error? Show the code where you create the
derived object and call DoStuff. There's no reason that virtual functions
should not work with templates.

john
 
S

Sharad Kala

Peter Hagenaar said:
Hello there.


template<class T>
class BaseClass
{
public:
virtual void DoStuff(void)
{
MessageBox("C++ BaseClass");
};

T MyVar;

};


and a derived class of the form:

class DerivedClass : public BaseClass<MyType>

What is MyType ? Is it a UDT or a template parameter ? If template parameter
then template said:
{
public:
virtual void DoStuff(void)
{
BaseClass<MyType>::DoStuff(); // this one is called

MessageBox("C++ DerivedClass"); // not executed !

int dummy = 0; // not executed !

dummy += 23456; // not executed !
};

};

Post the minimal code that demonstrates your problem exactly. Try not to use
non-standard class like MessageBox, use std::cout instead.

Sharad
 
A

Arne Adams

[email protected] (Peter Hagenaar) wrote in message news: said:
virtual void DoStuff(void)
{
BaseClass<MyType>::DoStuff(); // this one is called

MessageBox("C++ DerivedClass"); // not executed !

int dummy = 0; // not executed !

dummy += 23456; // not executed !
};

};

Could it be that you are calling DoStuff on a Copy of a
BaseClass<MyType> object (e.g. could slicing be the problem here?)

Arne
 
Z

Zaharije Pasalic

I compiled this example, and it works fine:

// CODE
#include <iostream>
using namespace std;

template<class T> class BaseClass
{
public:
virtual void DoStuff(void)
{
cout << "C++ BaseClass";
};
T MyVar;
};

class DerivedClass : public BaseClass<int>
{
public:
virtual void DoStuff(void)
{
BaseClass<int>::DoStuff();
cout << "C++ DerivedClass";
int dummy = 0;
dummy += 23456;
};
};


int main()
{
DerivedClass a;
a.DoStuff();
return 0;
}
// END-CODE


What kind of compiler You use?
 
P

Peter Hagenaar

Thanks you all for your reply.

After your replies I came to the conclusion that it had to be possible so
i created a console application in MS VC++ and it worked !

Of course I could have done this in the first place but I wrote the
example-code
for the newsgroup and the odd behavior gave me the idea that what I wanted
wasn't supported.

I don't know why it didn't work in the first place but I probably made a
mistake somewhere and overlooked something.

Thank you for contributing.

Regards ,


Peter Hagenaar.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top