Jump to content
  • Advertisement
Sign in to follow this  
Skarsha

Interesting Template.

This topic is 4578 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

I'm not sure if this is known or used, but it seems kinda novel to me. I don't know how useful it would be.
#include <stdio.h>

template <typename T>
class C : public T {
  public:
	C() {}
	~C() {}
	void foo() { y++; }

  private:
    int y;
};

class B {
  public:
	virtual ~B() {}

	virtual void foo() { x++; }
	virtual void bar() { x++; }
	void bam() { printf("%d", x); }

  protected:
	B() : x(0) {}

	int x;
};

int main(int argc, char* argv[]) {
	C<B> d;
	d.foo();
	d.bar();
	d.bam();
}

Compiles as is on MSVC++ 2005 EE. Note: The template class is inheriting the template parameter. Funky eh?

Share this post


Link to post
Share on other sites
Advertisement
There's even weirder:

template<class D> class Base { /* stuff using D */ };

class Derived : public Base<Derived> { /* stuff */ };


→ James Coplien. "Curiously Recurring Template Pattern". C++ Report, Feb. 1995

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!