Topic: Is this compiler conforming?


Author: johnw@jove.acs.unt.edu (John Robert Williams)
Date: 1995/04/09
Raw View
John Andrew Fingerhut (jaf3@ritz.cec.wustl.edu) wrote:
> A compiler used by one of my students does not output the line
> "base (const base &)" on this example program and I think that it should.
> Sun C++ 4.0 does.  Does the current draft standard allow the compiler to
> optimize away the copy constructor of base when derived is cast as a base
> object in its ouput function?

> #include <iostream.h>

> class base {
>  int b;
> public:
>  base (int i) { b = i; }
>  base (const base &copy) {
>   cout << "base (const base &)" << endl;
>   b = copy.b;
>  }
>  friend ostream &operator<< (ostream &o, const base &b) {
>   return o << b.b;
>  }
> };

> class derived : public base {
> public:
>  derived (int i) : base (i) { };
>  friend ostream &operator<< (ostream &o, const derived &d) {
>   return o << (base) d;

I'm no expert, but I think you just need to change the cast to (base &).

>  }
> };

John Williams
johnw@jove.acs.unt.edu
"Life is case sensitive."






Author: jaf3@ritz.cec.wustl.edu (John Andrew Fingerhut)
Date: 1995/04/06
Raw View
A compiler used by one of my students does not output the line
"base (const base &)" on this example program and I think that it should.
Sun C++ 4.0 does.  Does the current draft standard allow the compiler to
optimize away the copy constructor of base when derived is cast as a base
object in its ouput function?

#include <iostream.h>

class base {
 int b;
public:
 base (int i) { b = i; }
 base (const base &copy) {
  cout << "base (const base &)" << endl;
  b = copy.b;
 }
 friend ostream &operator<< (ostream &o, const base &b) {
  return o << b.b;
 }
};

class derived : public base {
public:
 derived (int i) : base (i) { };
 friend ostream &operator<< (ostream &o, const derived &d) {
  return o << (base) d;
 }
};

main ()
{
 cout << derived (3) << endl;
}
--
Stephen Gevers
sg3235@shelob.sbc.com