By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,106 Members | 1,099 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,106 IT Pros & Developers. It's quick & easy.

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

P: n/a
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<intmethod() { }
};

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?

--
Best regards,
Vilar Camara Neto

Mar 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
vi*******@gmail.com wrote:
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<intmethod() { }
};

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
Mar 25 '07 #2

P: n/a
On 25 mar, 14:29, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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?
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.

--
Regards,
Vilar Camara Neto

Mar 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.