Templates and inheritance in g++.

M

Maciej Zobniow

Hi everybody!

I have problem with usage of inheritance and templates in my program
compiled under g++.
I have a base class A:

template<class T>
class A {
public:
A();
A(const A& orig);
virtual ~A();

private:

};

and child class B:

clacc B : public A<uint8_t>{
public:
B();
B(const B& orig);
~B();
}

The class A is initializing a structure, then B adds some buffers to the
structure. In implementation of construction of B I can write:

B::B() : A<uint8_t>(){
....
}
ant this works.
The problem happens in destructor. As I read in all books and
turtorials, destructor of A is called just after destructor of B
finished. Then I've implemented the destructor of B in the way that it
frees only buffers allocated in B(). The destructor of A do the rest.
As I can't write similar to constructor:
B::~B():~A<uint8_t>(){...}

in my code I have:

B::~B(){
....
}

Program compiles, but linking fails with a message:

undefined reference to A::~A<unsigned char>()

I guess that if compiler do not see any implicit reference to
A::~A<uint8_>() it's do not compile the specialization of the template.
How to force the compiler to compile the specialization in the best way?

Thanks
Maciek
 
R

Robert Fendt

Program compiles, but linking fails with a message:

undefined reference to A::~A<unsigned char>()

I guess that if compiler do not see any implicit reference to
A::~A<uint8_>() it's do not compile the specialization of the template.
How to force the compiler to compile the specialization in the best way?

Just a hunch, but s the destructor defined outside the class
template? Then it's not surprising no code is generated for it.
You can either pull it inside the template, or explicitely mark
it inline (which should cause the destructor template
to be instantiated along with the rest of the class).

Regards,
Robert
 
M

Maciej Zobniow

Right!

Thx & Rgrds
Maciek


Robert said:
Just a hunch, but s the destructor defined outside the class
template? Then it's not surprising no code is generated for it.
You can either pull it inside the template, or explicitely mark
it inline (which should cause the destructor template
to be instantiated along with the rest of the class).

Regards,
Robert
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top