| 3.
template |
|||||||
|
30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied. Book (308 Pages):
![]() Video (One Hour):
![]() Resume Review (24 - 48hr)
All Products / Services
|
|||||||
3.
template <typename T>
class Foo
{
T tVar;
public:
Foo(T t) : tVar(t) { }
};
class FooDerived : public Foo<std::string> { };
FooDerived fd;
What is preventing the above code from being legal C++?
Choice 1
FooDerived uses the non-C++ type std::string.
Choice 2
FooDerived is a non-template class that derives from a template class.
Choice 3
The initialization of tVar occurs outside the body of Foo's constructor.
Choice 4
tVar is a variable of an unknown type.
Choice 5
A constructor must be provided in FooDerived.
4
I agree
Thomas, is this some kind of online test? or at onsite?
There is no default ctor for Foo so we can either:
- create a default ctor for Foo or
- create a ctor in FooDerived that will be call the existing non-default Foo ctor.
The following works for example...
class FooDerived : public Foo<std::string> {
public:
FooDerived(std::string str = "test"): Foo<std::string>(str) { }
};int main(int argc, char * argv[])
{FooDerived d;
return 0;
}
Choice 5. No default constructor for Foo, so we can not construct FooDerived.