This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problem with base and derived classes.


Dera GCC developers,

I have two problems with the code attached below under egcs-1.1.2,
gcc-2.91.1. The first one concerns missed message from the compiler about
"incomplete type" for D,E classes and the second one is about assignmen to
base class pointer.

See comments in the code.

--- CUT here:  File file1.c -----------
class A            {public: };
class B            {public: friend class D; friend class E;};
class D : public A {public:               }; // Comment this line.
class E : public A {public: E(const B &b);}; // Comment this line.

int main(void)
{
  A *a; // Pointer to base class.

  // This attempt to call non-existed constructor is successful!
  D d1(B()); // The compiler message is expected here if D is undefined.
  a = &d1;   // Error.
  
  D d2;      // OK. We get error message if D is undefined.
  a = &d2;   // OK.

  E e(B());  // The compiler message is expected here if E is undefined.
  a = &e;    // Error. Why?
}
--- CUT here:  File file1.c -----------

$ g++ -c file1.c
file1.c: In function `int main()':
file1.c:12: assignment to `A *' from `D (*)(B (*)())'
file1.c:18: assignment to `A *' from `E (*)(B (*)())'

If classes D and E are commented:
$ g++ -c file1.c
gams:~/tmp> g++ -c file1.c
file1.c: In function `int main()':
file1.c:12: assignment to `A *' from `D (*)(B (*)())'
file1.c:14: aggregate `class D d2' has incomplete type and cannot be
initialized
file1.c:18: assignment to `A *' from `E (*)(B (*)())'

With best wishes,
Alexander.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]