Topic: Forward class declarations stricter in


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 9 Jan 1995 21:18:50 GMT
Raw View
In article 0901951031300001@nonlinear.mcom.com, atotic@mcom.com (Aleksandar Totic) writes:
>
>Here is a real example that does produce an error:
>
>class A {
>public:
>    class B * b; // 3
>};
>
>class B {  // 6
>public:
>    short b1;
>    short b2;
>};
>
>main() {
>    B * b;  // 13
>    A * a;
>    a->b = b;
>}
>
>Error   : type mismatch
>test.cp line 15   a->b = b;

The language rules have changed somewhat in this area, so the results you
get will be different with an older compiler than with a newer one -- at
least with one written to current rules. Your compiler is evidently newer.

Under current rules, line 3 declares B to be a class defined in the scope
of A, but not yet seen. That is, the qualified name of the class is A::B.

Line 6 defines a class B at file scope, which is the B referred to in line 13.
Thus, the type of a->b is A::B and the type of b is ::B, which are not
the same type.

If you want to forward-declare a file-scope class name, do it at file scope:

class B; // forward declaration

class A {
public:
 B* b; // now refers to the file-scope B
};

class B { .... }; // definition of B
---
Steve Clamage, stephen.clamage@eng.sun.com