A
Andreas Wachowski
Hi,
consider the following program:
// --------------
class A
{
public:
A(int i) { }
};
class B
{
public:
B(const A&) { }
void f(int) const { }
};
int main()
{
int i(1);
// Declaring a variable v of type B, initialized with A(i):
B v(A(i)); // Nope, this is not producing the intended behaviour, see
error messages below
// Working alternatives:
// 1) B v((A)(i));
// 2) B v(i);
// 3) B v(1);
// 4) B v = B(A(i));
v.f(i); // Sun Workshop 5.2: B(*)(A) is not a structure type
// GCC 3.3.1: request for member b in b(A), which
// is of non-aggregate type B()(A)
return 0;
}
// --------------
As far as I understand, "B v(A(i));" is interpreted as a "pointer v to a
function taking a parameter of type A and returning B". In that case,
naturally, f cannot be called on v.
I don't understand, however, why "B v(A(i));" shouldn't equally well be seen
as "variable v of type B, initialized with the temporary A(i)". For example,
what's the difference to alternative 1), "B v((A)(i));" ?
Do I miss something trivial? I would appreciate if someone could elaborate
in detail on how this declaration is interpreted.
Many thanks,
Andreas
consider the following program:
// --------------
class A
{
public:
A(int i) { }
};
class B
{
public:
B(const A&) { }
void f(int) const { }
};
int main()
{
int i(1);
// Declaring a variable v of type B, initialized with A(i):
B v(A(i)); // Nope, this is not producing the intended behaviour, see
error messages below
// Working alternatives:
// 1) B v((A)(i));
// 2) B v(i);
// 3) B v(1);
// 4) B v = B(A(i));
v.f(i); // Sun Workshop 5.2: B(*)(A) is not a structure type
// GCC 3.3.1: request for member b in b(A), which
// is of non-aggregate type B()(A)
return 0;
}
// --------------
As far as I understand, "B v(A(i));" is interpreted as a "pointer v to a
function taking a parameter of type A and returning B". In that case,
naturally, f cannot be called on v.
I don't understand, however, why "B v(A(i));" shouldn't equally well be seen
as "variable v of type B, initialized with the temporary A(i)". For example,
what's the difference to alternative 1), "B v((A)(i));" ?
Do I miss something trivial? I would appreciate if someone could elaborate
in detail on how this declaration is interpreted.
Many thanks,
Andreas