为什么我得到的构造函数和复制构造函数在这里调用? [英] Why I am getting the Constructor and the copy constructor called here?

查看:126
本文介绍了为什么我得到的构造函数和复制构造函数在这里调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多C#代码,我必须写在C + +。我在C ++中没有太多的经验。

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

我使用 Visual Studio 2012 来构建。该项目是一个 C ++中的静态库(而不是C ++ / CLI)。

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?

这是我的代码
$ b

This is my code

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

当方法MyInstance被调用时:

When the Method MyInstance gets called:


  1. 首先,调用MyClass的构造函数

  2. 然后是CopyConstructor

  3. myInstance线。

可能myLocalVariable持有的实例只是暂时的,可以在以后被销毁?

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

更新:

由于有些人无法重现我的问题,我在此处添加更多代码。我现在有3个项目具有相同的行为(一个是静态库我从我的单元测试,其他2是Win32控制台应用程序调用)

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 ++主要

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

    return 0;
}

C ++ MyClass.h b
$ b

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;
}

我的输出:
$ b

My Output:


Myinstance

Myinstance

参数缺点

缺点

解构方


推荐答案

只需将变量写为

static MyClass instance;

,或

static MyClass instance(foo, bar);

或者由于这是MSVC 2012,可能支持统一的初始化语法:

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.)


被保持的myLocalVariable只是暂时的

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

请注意,首先,正确的单词是临时,第二, myLocalVariable 实例,它不包含一个。除非你明确使用指针或引用,C ++对象的行为更像C#structs比类,因为变量不保存引用,他们真的是对象。

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.

这篇关于为什么我得到的构造函数和复制构造函数在这里调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆