Why I am getting the Constructor and the copy constructor called here? [duplicate]

孤街浪徒 提交于 2020-01-06 12:38:27

问题


I have a lot of C# Code that I have to write in C++. I don't have much experience in C++.

I am using Visual Studio 2012 to build. The project is an 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 can see both the Constructor and the Copy Constructor being called. I don't understand why both get called, I thought only one would. Is there a way that I could make only one constructor get called?

This is my code

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

When the Method MyInstance gets called:

  1. First the Constructor of MyClass is called
  2. Then the CopyConstructor
  3. And then the "return myInstance" line.

Is it possible that the instance being held by myLocalVariable is only temporal and could get destroyed later on?

Update:

Since some people can not reproduce my problem, I am adding here more code. I have now 3 projects with the same behavior (one is a static library I am calling 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 Output:

Myinstance

Parameters Cons

Copy Cons

Destructor


回答1:


Just write the variable as

static MyClass instance;

if there are no parameters, or

static MyClass instance(foo, bar);

if there are. Or since this is MSVC 2012, the uniform initialization syntax might be supported:

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

This will initialize the variable in-place, instead of initializing a temporary and copying it to the target. (The compiler would be permitted to omit the copy, but apparently it doesn't in your case.)

Is it possible that the instance being held by myLocalVariable is only temporal

Note that first, the correct word is "temporary", and that second, myLocalVariable is the instance, it doesn't hold one. Unless you explicitly use pointers or references, C++ objects behave more like C# structs than classes, in that the variables don't hold references, they really are the objects.




回答2:


Is it possible that the instance being held by myLocalVariable is only temporal and could get destroyed later on?

myLocalVariable is static hence will not be destroyed and would be available each time you make call to MyInstance

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

This would only invoke 1 constructor which is param constructor, as myLocalVariable is being returned by reference and not by value.



来源:https://stackoverflow.com/questions/20144935/why-i-am-getting-the-constructor-and-the-copy-constructor-called-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!