I’m having a bit of trouble using virtual functions in C++, and I might be misusing them in a constructor. The problem is that when linking a component lib (written by me) into my final executable, a virtual function is marked as undefined, even though I have written an implementation for it, and linked it.
I have the following class:
template<class BufferType, class ConnectionType, class HandlerType>
class UdpConnection
{
public:
UdpConnection(size_t dispatchCount) : service(),
listener(service),
pool(dispatchCount), sysMsgHandlers(),
bufferPool(), buffers()
{
assert(dispatchCount > 0);
initBuffers(dispatchCount);
initSysHandlers();
}
protected:
virtual void initSysHandlers() = 0;
}
In my subclass:
class UdpClient : public UdpConnection<SyncBufferHandler, UdpClient, ClientNetworkHandler>
{
protected:
void initSysHandlers();
}
And the subclass source file:
void UdpClient::initSysHandlers()
{
}
As you can see, I am calling a virtual function in my constructor. As far as I can tell, this should be fine, since I am aware that my subclass constructor won’t have been called, so I can’t use any instance variables, but I simply add a few sub-class specific items to a std::map.
Linking CXX static library libnetwork.a
[ 75%] Built target network
Scanning dependencies of target testclient
[ 87%] Building CXX object CMakeFiles/testclient.dir/src/test/testclient.cpp.o
Linking CXX executable testclient
src/network/libnetwork.a(udpclient.cpp.o): In function `voip::network::UdpConnection<voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler>::UdpConnection(unsigned long)':
udpclient.cpp:(.text._ZN4voip7network13UdpConnectionINS0_6client17SyncBufferHandlerENS2_9UdpClientENS2_20ClientNetworkHandlerEEC2Em[voip::network::UdpConnection<voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler>::UdpConnection(unsigned long)]+0x10d): undefined reference to `voip::network::UdpConnection<voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler>::initSysHandlers()'
collect2: ld returned 1 exit status
What am I doing wrong here? Please ask if you need more information, wanted to keep it as short as possible!
You are calling the virtual function from the base class constructor. There are special rules for the dispatch of virtual functions during construction and destruction:
Effectively, when the constructor of the base class
UdpConnectionis executing, the dynamic type of the object isUdpConnection, notUdpClient, so the final overrider of the virtual function that is selected is the one forUdpConnection, not the most derived class,UdpClient.This means that when you call
initSysHandlers()in theUdpConnectionconstructor, it isUdpConnection::initSysHandlers()that gets called, ever time, not the override in the most derived class. Because you haven’t provided a definition ofUdpConnection::initSysHandlers(), you get the linker error.The expert advice is that you should “Never Call Virtual Functions during Construction or Destruction”.