I have a base class First
and a derived class Second
. In the base class there's a member function create
and a virtual function run
. In the constructor of Second
I want to call the function First::create
, which needs access to the implementation of its child class' run()
function. A colleague recommended using function templates since First
can't know it's child classes explicitly. Sounds weird? Here's some code:
First.h
#pragma once
#include <boost/thread/thread.hpp>
#include <boost/chrono/chrono.hpp>
class First
{
public:
First();
~First();
virtual void run() = 0;
boost::thread* m_Thread;
void create();
template< class ChildClass >
void create()
{
First::m_Thread = new boost::thread(
boost::bind( &ChildClass::run , this ) );
}
};
First.cpp
#include "First.h"
First::First() {}
First::~First() {}
Second.h
#pragma once
#include "first.h"
class Second : public First
{
public:
Second();
~Second();
void run();
};
Second.cpp
#include "Second.h"
Second::Second()
{
First::create<Second>();
}
void Second::run()
{
doSomething();
}
I'm getting an error at First::create<Second>();
saying Error: type name is not allowed. So what's the reason for this error? I assume I didn't quite get the whole mechanics of templates, yet - I'm very new to this topic.