My question is about the new C++11 feature Delegating Constructors. So I have two similar ctors in my class and I would like to simplify their implementation. The problem that they both have structures as a parameter and when I tried to delegate them, a compiler error occured:
error: type ‘MyClass’ is not a direct base of ‘MyClass’
So here is before:
MyClass::MyClass ( const timeval & TV ) :
Seconds ( TV.tv_sec),
USeconds ( TV.tv_usec ),
{
}
MyClass::MyClass ( const timespec & TS ) :
Seconds ( TS.tv_sec),
USeconds ( TS.tv_nsec * 1000 ),
{
}
After:
MyClass::MyClass ( const timeval & TV ) :
MyClass ( timeval { TV.tv_sec, TV.tv_usec/1000 } )
{
}
MyClass::MyClass ( const timespec & TS ) :
Seconds ( TS.tv_sec),
USeconds ( TS.tv_nsec * 1000 ),
{
}
Does anybody know how can I call the second ctor from the first one correctly?
Besides the obvious error (you are trying to delegate to the same constructor) the code is correct and should work on g++4.7
It might be an issue with your compiler/version.