This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Copy constructor not called.


Hi Mustafa,
 
> Please consider the following program:
> 
> #include <iostream>
> using namespace std;
> class A
> {
> public:
>    A ()
>    {
>       cout << "constructor" << endl;
>    }
>    A (A &a)
>    {
>       cout << "copy constructor" << endl;
>    }
>    A operator = (const A &a)
>    {
>       cout << "= operator" << endl;
>    }
> };
> int main()
> {
>    A a;
>    A b;
>    b = ( b = a );
> }

Your "copy constructor" is not an appropriate copy constructor.

Change it to:

A(A const& a)
{
  cout << "copy constructor" << endl;
}

Also, your assignment operator is not an appropriate assignment operator.

Change it to:

A const& operator = (A const& a)
{
  cout << "= operator" << endl;
  return *this;
}

HTH,
--Eljay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]