我从基础模板类继承,需要覆盖派生模板类中的onConnect
方法。如何确保从派生模板实例中覆盖第二个bind
中调用的方法?
以下是基本模板类的片段。
class connector()
{
typedef boost::asio::ip::tcp::resolver::iterator resolver_type;
void start();
void onConnect(const boost::system::error_code& ec, resolver_type::iterator i);
void connectCondition(const boost::system::error_code& ec, resolver_type::iterator i);
};
void Connector::start(){
async_connect(socket, i,
boost::bind(&Connector::connectCondition, this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator),
boost::bind(&Connector::onConnect, this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
}
void Connector::connectCondition(const boost::system::error_code& ec, resolver_type::iterator i)
{
// test connection
}
void Connector::onConnect(const boost::system::error_code& ec, resolver_type::iterator i)
{
std::cout << "called onConnect from base template class << std::endl;
}
目前,此代码将始终从基本模板类调用onConnect
方法,而不是派生模板类。
class DerivedConnector : public Connector
{
typedef boost::asio::ip::tcp::resolver::iterator resolver_type;
//use base connector start method
void start();
//override onConnect
void onConnect(const boost::system::error_code& ec, resolver_type::iterator i);
};
void DerivedConnector::start()
{
std::cout << "Calling base start method" << std::endl;
Controller::start();
}
void DerivedConnector::onConnect(const boost::system::error_code& ec, resolver_type::iterator i)
{
std::cout << "called onConnect from derived template class" << std::endl;
}
我尝试使用this->onConnect
无效,因为编译器抱怨函数调用不匹配。