0

According to this question a copy constructor is called when the object is passed by value into another function.

I removed my copy constructor as a test but can still pass it by value. Does this make sense?

I commented out the copy constructor:

 /*
Matrix4(const Matrix4<T>& m)
{
    x.x = m.x.x; x.y = m.x.y; x.z = m.x.z; x.w = m.x.w;
    y.x = m.y.x; y.y = m.y.y; y.z = m.y.z; y.w = m.y.w;
    z.x = m.z.x; z.y = m.z.y; z.z = m.z.z; z.w = m.z.w;
    w.x = m.w.x; w.y = m.w.y; w.z = m.w.z; w.w = m.w.w;
}
 */

typedef Matrix4<float> mat4;

Then this compiles and runs fine:

void ttt(mat4 hi){

}

void yyy(){
    mat4 x;
    ttt(x);
}

So perhaps the copy constructor is not called in such a case? What is happening here?

Community
  • 1
  • 1
johnbakers
  • 24,158
  • 24
  • 130
  • 258

2 Answers2

3

If you don't provide a copy constructor, the compiler generates one for you, which gets used when you pass by value (in C++11 there is also the possibility that a move copy constructor gets called in certain circumstances.) To really disable it, you should declare it private, or delete it in C++11:

class Foo
{
 private:
  Foo(const Foo&);
};

class Foo11
{
 public:
  Foo(const Foo&) = delete;
};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

If there are no constructors specified, C++ creates a parameterless and a copy constructor by itself. It simply performs a copy of the object.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • C++ doesn't generate anything, since it's an abstract concept, a programming language. The thing that generates the constructors is the compiler. –  Apr 06 '13 at 06:44
  • 1
    'performs a member-wise copy of the object' is the normal way of expressing it. There are many ways to copy and the compiler generated default may not be the right one. – john Apr 06 '13 at 06:45
  • @H2CO3 C++ mandates that the compiler generates the constructor :) – juanchopanza Apr 06 '13 at 06:45
  • @juanchopanza That's exact. –  Apr 06 '13 at 06:45
  • why does the compiler generate a default copy constructor in such a case but not also generate a default overload of = since they are often the same. – johnbakers Apr 06 '13 at 06:50
  • 1
    It does generate a default overload of = under normal circumstances. Not sure where you're getting your information from. – john Apr 06 '13 at 06:53
  • Well my class does not have an overload of the = but when I attempted to set an object of this class = to another of the same class, the compiler broke with error that there was no overload of =. So I am writing the overload now. – johnbakers Apr 06 '13 at 06:59
  • This is a misleading answer since it might be interpreted to mean that defining *any* constructor will suppress automatic generation of the default constructor and copy constructor, and that's not true. – jamesdlin Apr 06 '13 at 07:03
  • @john my issue with = is resolved; forgot that it was included in a `const` function and it was used with an ivar. whoops. thanks. – johnbakers Apr 06 '13 at 07:07