470,596 Members | 1,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

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

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
2 4049
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
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.

Similar topics

12 posts views Thread by lothar | last post: by
3 posts views Thread by Mario | last post: by
4 posts views Thread by bwmiller16 | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
12 posts views Thread by puzzlecracker | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.