Why am I calling the copy constructor and constructor here?

I have a lot of C # code that I have to write in C ++. I have little experience in C ++.

I am using Visual Studio 2012 to build. The project is a Static Library in C ++ (not in C ++ / CLI).

I have a class with a static method with a static private member. When I debug, I see both the Constructor creator and the Copy Constructor invoked. I do not understand why both are called, I thought that only one would be. Is there a way I can force to call only one constructor?

This is my code.

MyClass& MyClass::MyInstance() { static MyClass myLocalVariable = MyClass(/*parameters*/); return myLocalVariable ; } 

When calling the MyInstance method:

  • MyClass constructor is first called
  • Then CopyConstructor
  • And then the line "return myInstance".

Is it possible that the instance stored by myLocalVariable is only temporary and can be destroyed later?

Update:

Since some people cannot reproduce my problem, I am adding more code here. I have 3 projects with the same behavior (one of them is a static library, which I call from my Unit tests, and the other 2 are Win32 Console Applications)

C ++ Main

 int _tmain(int argc, _TCHAR* argv[]) { MyClass& instance = MyClass::MyInstance(); return 0; } 

C ++ MyClass.h

 #pragma once #include <string> using namespace std; class MyClass { private: string name; public: MyClass(void); MyClass(string name); MyClass(const MyClass&); ~MyClass(void); static MyClass& MyInstance(); }; 

C ++ MyClass.cpp

 #include "MyClass.h" #include <iostream> using std::cout; MyClass::MyClass(void) { cout << "Empty Cons\n"; } MyClass::MyClass(string name) { this->name = name; cout << "Parameters Cons\n"; } MyClass::MyClass(const MyClass& myClass) { name = myClass.name; cout << "Copy Cons\n"; } MyClass::~MyClass(void) { cout << "Destructor\n"; } MyClass& MyClass::MyInstance() { cout << "MyInstance\n"; static MyClass myInstance = MyClass("MyClassName"); return myInstance; } 

My conclusion:

Myinstance

Minus options

Copy cons

destructor

0
c ++ visual-studio-2012
source share
2 answers

Just write the variable as

 static MyClass instance; 

if there are no parameters, or

 static MyClass instance(foo, bar); 

if there. Or, since this is MSVC 2012, a uniform initialization syntax may be supported:

 static MyClass instance{/*args here, could be empty*/}; 

This will result in initializing the variable in place instead of initializing the temporary and copying it to the target. (The compiler will be allowed to omit the copy, but apparently this is not so.)

Is it possible that the instance stored by myLocalVariable is only temporary

Note that firstly, the correct word is "temporary", and secondly, myLocalVariable is an instance, it does not hold it. Unless you explicitly use pointers or references, C ++ objects behave more like C # structures than classes, since variables do not contain references, they really are objects.

+3
source share

Is it possible that the instance stored by myLocalVariable is only temporary and can be destroyed later?

myLocalVariable is static, so it will not be destroyed and will be available every time you call MyInstance

 MyClass& MyClass::MyInstance() { static MyClass myLocalVariable = MyClass(/*parameters*/); return myLocalVariable ; } 

This will only call 1 constructor, which is a param constructor, since myLocalVariable is returned by reference, not by value.

+1
source share

All Articles