jdg@sisd.kodak.com (Jeff Gortatowsky CUST) (07/25/90)
Like many others I'm just getting started using g++ and TC++. Here's
the question in code (shortened quite a bit).
class A { ....... };
class B { ...... };
// C inherits A and B
class C : public A, public B { .... }; // Note: has class B
// D inherits only A
class D : public A { ..... }; // Note: no class B
// E should have everthing. 2 copies of A?? Better fix that virtually!
class E : public C, public D { .... };
main ()
{
E myObject(); // Construct a class E object
B *ptrToB; // Pointer to class B object
ptrToB = &myObject; // Illegal???? TC++ say it is.
ptrToB = (B *) &myObject; // legal????
}
Is the first assignment legal? One of the base classes of class E is
class C which did inherit a class B object. However the other class
inherited by class E was class D which did not inherit a class B object.
Is that why the first assignment is wrong? The second works, it
even runs right. I just want to know why if there is a base class
object of type B in an object of class E I can't use a base class pointer
without a cast. Please excuse any obvious stupidity on my part, I'm
just starting to make the shift.
--
Jeff Gortatowsky-Eastman Kodak Company .....uunet!atexnet!kodak!elmgate!jdg
(716)-726-0084
Eastman Kodak makes film not comments. Therefore these comments are mine
not theirs.
jeh@cs.rit.edu (Jim Heliotis) (08/02/90)
From article <1990Jul25.121614.16958@sisd.kodak.com>, by jdg@sisd.kodak.com (Jeff Gortatowsky CUST): > > class A { ....... }; > class B { ...... }; > class C : public A, public B { .... }; > class D : public A { ..... }; > class E : public C, public D { .... }; > > main () > { > E myObject(); // Construct a class E object > B *ptrToB; // Pointer to class B object > > ptrToB = &myObject; // Illegal???? TC++ say it is. > } I ran this under cfront 2.1. Note the error it gives: CC mi.C AT&T C++ Compiling System, v2.1 CC mi.C: "mi.C", line 17: error: no standard conversion of E (*)() to B * 1 error "E myObject();" is the declaration of a function; "E myObject;" would be the declaration of an object variable, initialized by a parameterless constructor (which, by the way, compiles fine)! Jim Heliotis Rochester Institute of Technology Rochester, NY 14623-0887 jeh@CS.RIT.EDU {allegra,seismo}!rochester!rit!jeh