Copy constructors

B

Bruno Ripa

Hallo,

imagine to have the following simple code:

#include <iostream>

class C
{
public:
C() {}
C( const C & Object )
{
std::cout<<std::endl<<"constant CC !";
}
C( C & )
{
std::cout<<std::endl<<"NON-costant CC!";
}
C & operator = ( const C & Object )
{
std::cout<<std::endl<<"constant =";
}
C & operator = ( C & Object )
{
std::cout<<std::endl<<"NON-costant =";
}

};

C test()
{
C dummy;
return dummy;

}

int main( int argc, char ** argv )
{
C t;
t = test();
return EXIT_SUCCESS;

}

Let's talk about test() function, in particular the 'return dummy'
instruction: should it not invoke a copy constructor to create a copy
of dummy object ?? Instead of this behaviour, i have no cc invoked. I
think it's not that normal!

I'm using g++ 3.3.x under debian, handling the project via Anjuta.

The question is that i have a Matrix class (it represents an image)
that owns a (int *) private member acting as the set of points of the
image.

The question is, having many methods returning a Matrix by value, i
have implemented all of:

Matrix( Matrix & M)
Matrix( const Matrix & M)
M & operator = ( const Matrix &)
M & operator = ( Matrix &)


to be sure to handle what is to be handled regarding the private
pointer (it's a dynamic buffer), but i can't figure which method is
called and i can only get objects with null buffers when methods
return.

Which can be the problem ??? A compiler optimization ??? A bug ???

Thanks in advance,
Bruno Ripa
 
V

Victor Bazarov

Bruno said:
imagine to have the following simple code:

I'm imagining... Real hard... Done!
#include <iostream>

class C
{
public:
C() {}
C( const C & Object )
{
std::cout<<std::endl<<"constant CC !";
}
C( C & )
{
std::cout<<std::endl<<"NON-costant CC!";
}
C & operator = ( const C & Object )
{
std::cout<<std::endl<<"constant =";
}
C & operator = ( C & Object )
{
std::cout<<std::endl<<"NON-costant =";
}

};

C test()
{
C dummy;
return dummy;

}

int main( int argc, char ** argv )
{
C t;
t = test();
return EXIT_SUCCESS;

}

Let's talk about test() function, in particular the 'return dummy'
instruction:

"Statement", not "instruction".
> should it not invoke a copy constructor to create a copy
of dummy object ?? Instead of this behaviour, i have no cc invoked. I
think it's not that normal!

It's called "NRVO" (named return value optimization), IIRC. Look it up.
I'm using g++ 3.3.x under debian, handling the project via Anjuta.

Unless you're reporting a bug, it shouldn't matter.
The question is that i have a Matrix class (it represents an image)
that owns a (int *) private member acting as the set of points of the
image.

That's not a question. That's a statement.
The question is, having many methods returning a Matrix by value, i
have implemented all of:

Matrix( Matrix & M)
Matrix( const Matrix & M)
M & operator = ( const Matrix &)
M & operator = ( Matrix &)


to be sure to handle what is to be handled regarding the private
pointer (it's a dynamic buffer), but i can't figure which method is
called and i can only get objects with null buffers when methods
return.

That's not a question either.

Implement the members as if they are called. In order to correctly manage
your dynamic memory, you don't need to know whether certain member
functions _are_ in fact called, only that they _might_ be.
Which can be the problem ??? A compiler optimization ??? A bug ???

Now there are three questions (or so it seems). The answer to the first
question is "any one of them can be a problem if you don't implement them
right". The answer to the second is "Yes", and the answer to the third is
"No".

Post real code with which you _do_ have a problem. Explain the problem.
Make sure to provide the input (if any) and desired output (if any) and
the description of the behaviour as it is and as it should be (and the
difference, if it's not obvious). Read the FAQ before all that.

V
 
D

deane_gavin

Bruno said:
Hallo,

imagine to have the following simple code:

#include <iostream>

class C
{
public:
C() {}
C( const C & Object )
{
std::cout<<std::endl<<"constant CC !";
}
C( C & )
{
std::cout<<std::endl<<"NON-costant CC!";
}
C & operator = ( const C & Object )
{
std::cout<<std::endl<<"constant =";
}
C & operator = ( C & Object )
{
std::cout<<std::endl<<"NON-costant =";
}

};

C test()
{
C dummy;
return dummy;

}

int main( int argc, char ** argv )
{
C t;
t = test();
return EXIT_SUCCESS;

}

Let's talk about test() function, in particular the 'return dummy'
instruction: should it not invoke a copy constructor to create a copy
of dummy object ?? Instead of this behaviour, i have no cc invoked. I
think it's not that normal!

I'm using g++ 3.3.x under debian, handling the project via Anjuta.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.9

is probably what's happening.
The question is that i have a Matrix class (it represents an image)
that owns a (int *) private member acting as the set of points of the
image.

The question is, having many methods returning a Matrix by value, i
have implemented all of:

Matrix( Matrix & M)

Why would you want to modify the source Matrix when making a copy?
Matrix( const Matrix & M)
M & operator = ( const Matrix &)

Do you mean
Matrix & operator = ( const Matrix &)
M & operator = ( Matrix &)

Again, do you mean
Matrix & operator = ( Matrix &)

And again why would you want to modify the source Matrix?
to be sure to handle what is to be handled regarding the private
pointer (it's a dynamic buffer), but i can't figure which method is
called and i can only get objects with null buffers when methods
return.

Which can be the problem ??? A compiler optimization ??? A bug ???

Eliding the copy when your "test" function returns is a permitted
optimisation. As for your problem with null buffers, that sounds like a
bug in your copy contructor and/or assignment operator, but without
seeing the code that's only a guess.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top