Re: Empty constructor confusion
On 2/3/2014 11:23 PM, Daniel wrote:
Environment is vs2010sp1 on Windows 7.
Consider the following:
template <typename C>
class A
{
public:
class B;
A()
{
}
A(const A& a)
{
}
void f()
{
std::cout << "Hello world" << std::endl;
}
};
template <typename C>
class A<C>::B : public A<C>
{
public:
B()
{
}
B(size_t n)
{
}
};
If I write
A<char> a(A<char>::B());
IIRC, it's interpreted as a declaration of a function 'a' that takes a
function [pointer] as an argument and returns A<char>. The argument is
a function with no arguments that returns a A<char>::B. Read up on
"C++ most vexing parse".
The solution is to surround the expression inside the parentheses in
another set of parentheses:
A<char> a((A<char>::B()));
The extra parentheses force the compiler to interpret the inner
expression as an expression instead of a declaration.
a.f();
compilation fails with "error C2228: left of '.f' must have class/struct/union"
But if I change that to
A<char> a(A<char>::B(10));
a.f();
compilation succeeds and the program runs.
Hmmm ... am I doing something wrong?
Yes. You didn't read the FAQ before posting. Read the FAQ.
V
--
I do not respond to top-posted replies, please don't ask