bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bug in 2.95.2?


From: Tommy McDaniel
Subject: Bug in 2.95.2?
Date: 16 Jul 2001 14:48:13 -0700

Please consider the following code:

PASS BY VALUE

#include <iostream>

using std::cout;
using std::endl;

class myClass {
public:
   myClass() {cout << "myClass default constructor called" << endl;}
   myClass(int num) { x = num; cout << "myClass conversion constructor
called" 
<< endl;}
   myClass(const myClass &obj) {cout << "myClass copy constructor
called" <<
endl;}
   ~myClass() { cout << "myClass destroyed" << endl;}
   int x;
};

class myClassB : public myClass {
public:
   myClassB() {cout << "myClassB default constructor called" << endl;}
   myClassB(myClass obj) { cout << "myClassB constructor called" <<
endl;}
   myClassB(myClassB &obj) { cout << "myClassB copy constructor
called" <<
endl;}
   ~myClassB() {cout << "myClassB destructor called" << endl;}
   int x;
};

void func(myClassB);

int main()
{
   myClass obj = 5;

   cout << obj.x << endl;
   func(obj);
   return 0;
}

void func(myClassB obj)
{
   obj.x = 10;
   cout << "In func, about to exit" << endl;
}

PASS BY REFERENCE

#include <iostream>

using std::cout;
using std::endl;

class myClass {
public:
   myClass() {cout << "myClass default constructor called" << endl;}
   myClass(int num) { x = num; cout << "myClass conversion constructor
called" 
<< endl;}
   myClass(const myClass &obj) {cout << "myClass copy constructor
called" <<
endl;}
   ~myClass() { cout << "myClass destroyed" << endl;}
   int x;
};

class myClassB : public myClass {
public:
   myClassB() {cout << "myClassB default constructor called" << endl;}
   myClassB(myClass obj) { cout << "myClassB constructor called" <<
endl;}
   myClassB(myClassB &obj) { cout << "myClassB copy constructor
called" <<
endl;}
   ~myClassB() {cout << "myClassB destructor called" << endl;}
   int x;
};

void func(const myClassB &);

int main()
{
   myClass obj = 5;

   cout << obj.x << endl;
   func(obj);
   return 0;
}

void func(const myClassB &obj)
{
   //obj.x = 10;
   cout << "In func, about to exit" << endl;
}

OUTPUT (SAME FOR BOTH)

myClass conversion constructor called
5
myClass copy constructor called
myClass default constructor called
myClassB constructor called
myClass destroyed
myClass copy constructor called
myClass default constructor called
myClassB constructor called
myClass destroyed
In func, about to exit
myClassB destructor called
myClass destroyed
myClassB destructor called
myClass destroyed
myClass destroyed

When I compile this under version 2.95.2, the output is as shown above
for both. What gets me is that there are two myClassB objects created,
even when pass by reference is used. I understand the creation of one
temporary, but why are there two? Is this a bug in this version of the
compiler or is it somehow supposed to create two myClassB objects,
even in pass by reference? It's like it creates a perfectly good
parameter and then creates another copy of it for the heck of it. Of
course, there may be a perfectly good reason for this, but if there is
I don't know it. Can anyone confirm whether this behavior is expected
or whether this is a bug? Thanks.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]