I have a base class of a template that has a constructor for converting from any other instance of an instance of this class, for example:
template <class T>
class FooBase
{
public:
FooBase()
{
}
template <class U>
FooBase(const FooBase<U>& other)
{
std::cout << "FooBase<U>" << std::endl;
}
};
Note that the copy constructor is not defined.
Then I have a derived template class that has a copy constructor, as well as a constructor used to convert:
template <class T>
class Foo : public FooBase<T>
{
public:
Foo()
{
}
Foo(const Foo& other) :
FooBase<T>(other)
{
}
template <class U>
Foo(const Foo<U>& other) :
FooBase<T>(other)
{
}
};
Since it FooBase
does not have a copy constructor, this leads to a FooBase<T>(other)
call to the copy instance created by the compiler. This means that if I run this:
int main()
{
Foo<int> a;
Foo<int> b(a);
return 0;
}
Exit is nothing when it should be FooBase<U>
.
, , FooBase
:
FooBase(const FooBase& other)
: FooBase<T>(other)
{
}
, , , , :
warning C4717: 'FooBase<int>::FooBase<int>': recursive on all control paths, function will cause runtime stack overflow
, .
?