Jump to content
  • Advertisement
Sign in to follow this  
deavik

Beginner C++: derive a class template from a templated type

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! Firstly, I must declare that my knowledge of C++ is not very great; but I have a question on inheritance and templates. Isn't it possible to derive a class template from a templated type? From the evidence of this page (the very last code snippet) it seems yes; but I can't get the following to compile with g++.
#include <iostream>

template< typename T >
class base {
	public:
	void set(T newval) { var = newval; }

	protected:
	T var;
};

template< typename T >
class derived : public base< T > {
	public:
	void holler() { std::cout << var << "\n"; }
};

int main()
{
	derived< float > inst;
	inst.set(4.0);
	inst.holler();

	return 0;
}

// Compiler says: `var' undeclared

The compiler, funnily enough, accepts the template argument but doesn't seem to be deriving from the base class? If I fix the template argument to the base class - it works. By this I mean:
template< typename T >
class derived : public base< float > {/*...*/};
I'm sure I'm missing something: could someone please point it out? Thanks [smile]

Share this post


Link to post
Share on other sites
Advertisement
Try:
template< typename T >
class derived : public base< T > {
public:
void holler() { std::cout << this->var << "\n"; }
};

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Try:
template< typename T >
class derived : public base< T > {
public:
void holler() { std::cout << this->var << "\n"; }
};

Well, I'll be darned! [smile]

Thank you sooo much, jyk!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!