Non-template class from a template base class with pure virtual methods

V

vilarneto

Hello everyone,

I'm facing a particular situation about template class derivation.
The subject is old -- deriving a non-template class from a template
base class -- but my problem is that the base class has a pure virtual
method. For instance:

template<class T>
class A {
public:
virtual void method() = 0;
};

class B : public A<int> {
public:
// neither of the following worked:
// void method() { }
// void method<int>() { }
// template<int> method() { }
};

int main(int argc, char *argv[]) {
B b;
return 0;
}

This is probably simple, but I couldn't figure out a way to implement
the derived method.

Also, I don't know if this matters, but I'd better stay apart from
compiler idiosyncrasies -- I'm searching for something portable, at
least between g++3.4.4 and VC7.

Does anybody have any hint about this?
 
K

Kai-Uwe Bux

Hello everyone,

I'm facing a particular situation about template class derivation.
The subject is old -- deriving a non-template class from a template
base class -- but my problem is that the base class has a pure virtual
method. For instance:

template<class T>
class A {
public:
virtual void method() = 0;
};

class B : public A<int> {
public:
// neither of the following worked:
// void method() { }

Actually, the above looks right and works for me. What error do you get?
// void method<int>() { }
// template<int> method() { }
};

int main(int argc, char *argv[]) {
B b;
return 0;
}

This is probably simple, but I couldn't figure out a way to implement
the derived method.

Also, I don't know if this matters, but I'd better stay apart from
compiler idiosyncrasies -- I'm searching for something portable, at
least between g++3.4.4 and VC7.

Does anybody have any hint about this?

Try the following with your compilers:

#include <iostream>

template<class T>
class A {
public:
virtual void method() = 0;
};

class B : public A<int> {
public:
void method() { std::cout << "hi!\n"; }
};

int main(int argc, char *argv[]) {
B b;
b.method();
return 0;
}


Best

Kai-Uwe Bux
 
V

vilarneto

Actually, the above looks right and works for me. What error do you get?

Shame on me, sorry. I was actually compiling+linking with gcc, which
was giving me linking errors. The problem is that "-lstdc++" isn't
implied when using gcc. (The fact that there were no compiling errors
gave me the impression that gcc would behave exactly as g++, according
to the source file extension.)

With g++ I've got no linking errors and everything works great.

Even so, your "this works for me" was a key point to understand the
problem. Thank you, Bux.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top