[gnu.g++] Constructing member objects?

c60c-2ca@e260-2d.berkeley.edu (Andrew Choi) (04/14/90)

In article <CIMSHOP!DAVIDM.90Apr3171447@uunet.UU.NET> cimshop!davidm@uunet.UU.NET (David S. Masterson) writes:
>I'm attempting what I thought was a simple thing, namely constructing an
>object that has member objects (in this case - one).  The following:
>
>class A {
>	...
>public:
>	A() {...}
>	~A() {...}
>};
>
>class B {
>	A a;
>	int x;
>public:
>	B(int n=0):A(n) { x = n; }
>	~B() {}
>};
>
>gets me a "A is not a Base type for B" error from G++ 1.36.  Am I using the
>right syntax or is this a bug with this compiler?
>
>--

You are using the wrong syntax.  class B should be defined as:

class B {
	A a;
	int x;
public:
	B(int n = 0) : a(n)	{ x = n; }
	~B()	{}
};

You use the form you have if and only if B is derived from A.



Andrew Choi
Internet Address:  c60c-2ca@web.berkeley.edu
Standard Disclaimer