Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Q: DEC C++: Conversion bug?
@ 1994-09-28 11:30 zeisel
  0 siblings, 0 replies; only message in thread
From: zeisel @ 1994-09-28 11:30 UTC (permalink / raw)


It is explictely said in the ARM that a pointer or a reference
of a derived class can be converted to a pointer
or a reference of its base class.
What about the object itself?
In a book (Guido Buzzi--Ferraris, "Scientifc C++") I read that it
cannot be converted.

I tried to test it with DEC C++ V1.3A-0 on a VAX and on an Alpha,
OpenVMS(V6.1), using the following program:

----------------------------
#include <iostream.h>

class Base
{
public:
#ifdef VIRTUAL
  virtual
#endif /* ifdef VIRTUAL */
  void identify() {cout << " Base " << endl;}
#ifdef CONSTRUCTOR
  Base()
  {
    cout << "This is Base()" << endl;
  }
  Base(const Base& a)
  {
    cout << "This is Base(const Base&)" << endl;
  }
#endif /* ifdef CONSTRUCTOR */
};


class Derived: public Base
{
public:
#ifdef VIRTUAL
  virtual
#endif /* ifdef VIRTUAL */
  void identify() {cout << " Derived " << endl;}
};

void f(Base* a)
{
  cout << "This is f" << endl;
  a->identify();
}
void g(Base& a)
{
  cout << "This is g" << endl;
  a.identify();
}
void h(Base a)
{
  cout << "This is h" << endl;
  a.identify();
}

int main()
{
   Base a;
   Derived b;

   f(&b);
   g(b);
   h(b);

   a = b;
   a = (Base)b;

   f(&a);
   g(a);
   h(a);

   return 0;
}
----------------------------

Compiling it, I get two times the same error message:
     h(b);
     a = (Base)b;
%CXX-E-PASNOCONCLA, (1) In this statement, "b" has a class type and cannot be
 converted to a class type that is incompatible.

Surprisingly enough, the program compiles well and gives the
expected results when I #define VIRTUAL, CONSTRUCTOR, or both,
or when I use Borland C++, Version 3.1.


What says the ARM?

5.4: Explicit Type Conversion, p.67

     Any type conversion not mentioned below and not explicitly
     defined by the user is an error.

     Any type that can be converted to another by a
     standard conversion (Chapter 4) can also be converted
     by explicit conversion.

4.7: The implicit conversions of a reference to a derived class
     to references to its base class
     implies implicit conversion of an object of a derived class
     to objects of its base classes.

Is this a bug in DEC C++?

By the way, DEC C++ calls the copy constructor of class Base
two times before it enters h(b);
Borland C++ calls it only one time.
Is this only a question of optimization?

Newsfeed unreliable, please answer also via EMail.

Helmut Zeisel
-------------------------------------------------------------------
Helmut Zeisel                         zeisel@indmath.uni-linz.ac.at
Institut fuer Mathematik
Universitaet Linz
A-4040 Linz                                  Tel  +43-732-2468-9225
AUSTRIA/EUROPE                               Fax     +43-732-246810



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1994-09-28 11:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-09-28 11:30 Q: DEC C++: Conversion bug? zeisel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox