-1

I have created a class and initialized an array of objects through constructor, why or why not copy constructor is getting called here? Is it copy elision?

#include<iostream>
#include<stdio.h>

class ABC
{
    int x, y;
public:
    ABC()
    {
        x = 0;
        y = 0;
    }
    ABC(int a,int b)
    {
        x = a;
        y = b;
    }
    ABC(const ABC &obj)
    {
        std::cout<<"Copy called";
    }
};

int main()
{
    ABC obj[2] = {ABC(), ABC(5,6)}; //copy elision or copy constructor?
}
Holt
  • 36,600
  • 7
  • 92
  • 139
Akankshi Mishra
  • 65
  • 2
  • 10
  • 1
    Please provide a compiling code... You are missing headers and all of your constructors are private. – Holt Jun 29 '16 at 09:36
  • And yes, this is due to copy-elision, try compiling with `-fno-elide-constructors` if you are using clang or g++. – Holt Jun 29 '16 at 09:36
  • 2
    Possible duplicate of [What are copy elision and return value optimization?](http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization) – Holt Jun 29 '16 at 09:37

1 Answers1

0

You're right, it's copy elision, the compiler does optimisations and creates objects right in place of memory allocated within array, hence copy constructor doesn't get called.

buld0zzr
  • 962
  • 5
  • 10
  • Thanks this is copy elision but can you elaborate it more, i have compile the prog with "-fno-elide-constructors" option then copy constructor is called. – Akankshi Mishra Jun 29 '16 at 09:50