Topic: wierd error in Ctor initialization list (MIL)


Author: John Lacey <johnl@vizdom.com>
Date: 1999/04/18
Raw View
miri wrote:
>
>   B::B (A & rA)
>   : ref_to_A(rA) // causes compilation error with xlC
>   {}
>
> this compiles nicely on VC++ 5.0

It also compiles with xlC using version 3.1.4.8 of the C++
compiler (AIX 4.2).

> does anyone know what's the reason for this error - i know the syntax is
> correct (it worked on VC5.0)...

The syntax is correct, and useful, but certainly compiling under
MSVC++ 5.0 is neither a sufficient nor a necessary condition for
correctness. Nearly all compilers implement a subset of the
standard with extensions, and MSVC++ is no exception.

John L
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: RA Scheltema <scheltem@kpn.research.com>
Date: 1999/04/13
Raw View
I don't quite understand why you want to do this. When I have a class-member
that can be a reference to an instance of a class, I always use pointers.
This way you extend readability (at least for me). What you're doing should
not be allowed. Try the following:

class B {
public:
   B(A *a);
private:
   A *a;
};

void main() {
   A a(9);
   B b(&a);
   // More code here :)
}


This will surely compile and not give you any warnings.
Hope this helps,
   Richard Scheltema.


miri wrote in message <370E533B.DF4210AC@netvision.net.il>...
>i have two classes:
>
>  class A {
>    int m_a;
>  public:
>    A(int a);
>    ~A();
>  };
>
>  class B {
>    A & ref_to_A;
>  public:
>    B(A & rA);
>   ~B();
>  };
>
>the implementation is
>  A::A (int a) {
>    m_a = a;
>  }
>
>  A::~A () {}
>
>  B::B (A & rA)
>  : ref_to_A(rA) // causes compilation error with xlC
>  {}
>
>  B::~B () {}
>
>this compiles nicely on VC++ 5.0
>
>however when i compile it on UNIX with xlC there is a compilation error
>on the constructor initialization list of class B...
>it says something like : "the data_member is neither a base class nor is
>it a non-static member"...
>
>does anyone know what's the reason for this error - i know the syntax is
>correct (it worked on VC5.0)...
>
>tnx
>Miri
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "J.Barfurth" <techview@bfk-net.de>
Date: 1999/04/13
Raw View
RA Scheltema schrieb in Nachricht <3712e6eb.0@pandora.research.kpn.com>...
>miri wrote in message <370E533B.DF4210AC@netvision.net.il>...

>>i have two classes:
>>
>>  class A {
>>    int m_a;
>>  public:
>>    A(int a);
>>    ~A();
>>  };
>>
>>  class B {
>>    A & ref_to_A;
>>  public:
>>    B(A & rA);
>>   ~B();
>>  };
[...]
>>  B::B (A & rA)
>>  : ref_to_A(rA) // causes compilation error with xlC
>>  {}

Looks fine to me. Seem like that compiler has a big problem with referenc=
es.

>I don't quite understand why you want to do this. When I have a
class-member
>that can be a reference to an instance of a class, I always use pointers.
>This way you extend readability (at least for me). What you're doing sho=
uld
>not be allowed. Try the following:
>
>class B {
>public:
>   B(A *a);
>private:
>   A *a;
>};

>
>void main() {
int main() - IT'S REQUIRED.

>   A a(9);
>   B b(&a);
>   // More code here :)
>}
This is something completely different. Having a reference implies that (=
1)
the reference must be initialized in the constructor, (2) the object
referred to will always be the same and (3) there has to be an object.

(1) and (2) can be mimicked by declaring the pointer 'A * const'. But (3)
cannot. If you require an object, you'd have to explicitly check for null
pointers. With your code B b(0) constructs a B having no A. This is not
legal in the original code.

(4) As the constructor is not declared explicit, it is also a conversion
constructor in this case. So this states that an A (we need a modifiable
lvalue in this case) can be converted into a B. Suppose you have a global
operator:
    double operator + (B const&,int);    // return type skips question of
copy c'tor
with the original code you can write:
    (a + 3);    // yields a double
but in your case
    (&a + 3)    // yields A* - or undefined behaviour, because &a doesn't
point to an array of at least 3 As

-- J=F6rg
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: miri <nmirs@netvision.net.il>
Date: 1999/04/13
Raw View
i have two classes:

  class A {
    int m_a;
  public:
    A(int a);
    ~A();
  };

  class B {
    A & ref_to_A;
  public:
    B(A & rA);
   ~B();
  };

the implementation is
  A::A (int a) {
    m_a = a;
  }

  A::~A () {}

  B::B (A & rA)
  : ref_to_A(rA) // causes compilation error with xlC
  {}

  B::~B () {}

this compiles nicely on VC++ 5.0

however when i compile it on UNIX with xlC there is a compilation error
on the constructor initialization list of class B...
it says something like : "the data_member is neither a base class nor is
it a non-static member"...

does anyone know what's the reason for this error - i know the syntax is
correct (it worked on VC5.0)...

tnx
Miri
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]