问题描述
我有很多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).
在C#代码中有一个类,创建实例
In the C# code there is a class for which some static instances are created
C#
namespace MyNamespace
{
public class MyClass
{
//Class Code
static public MyClass Instance1 = new MyClass(/*parameters*/);
static public MyClass Instance2 = new MyClass(/*other parameters*/);
我需要在C ++中做类似的操作,到目前为止:
I need to do something similar in C++, so far I got:
C ++
namespace MyNamespace
{
class MyClass
{
//Class Code
}//end of class
static MyClass& Instance1 = MyClass(/*parameters*/);
static MyClass& Instance2 = MyClass(/*other parameters*/);
}//end of Namespace
是不一样的,通过添加单词静态我做的是,我的Instance1和Instance2只在当前文件的范围内可见。这是正确的吗?
However, from what I read this is not quite the same, by adding the word "static" what I am doing is that my Instance1 and Instance2 are only visible within the scope of the current file. Is this correct?
所以我不想要一堆Instance1,我只想在整个程序中一个。
我读到,而不是使用 static ,我可以使用 extern ,告诉编译器变量的定义在另一个文件,因此我会结束只有一个实例Instance1和Instance2,而不是多个实例。
So I don't want a bunch of Instance1, i just want one in the entire program.I read that instead of using static, I could use extern, which tells the compiler the definition of the variable is in another file, thus I would wind up with only one instance of Instance1 and Instance2 and not multiple ones.
所以我试过:
C ++ MyClass.h
extern MyClass& Instance1;
extern MyClass& Instance2;
C ++ MyClass.cpp
MyClass& Instance1 = MyClass(/*parameters*/);
MyClass& Instance2 = MyClass(/*other parameters*/);
这个构建正常,但是当我尝试运行我的测试时, :
Failed to set up the execution context to run the test
在使用 extern 之前,我的测试运行正常,但是当我添加它时,它们将无法工作。这使我相信我不能正确地声明,或者Visual Studio 2012不能正确支持这些功能的一些功能?
Before using extern my tests ran just fine, but when I add it they won't work. Which leads me to believe I am not declaring things properly, or maybe Visual Studio 2012 doesn't support properly some of this functionality?
推荐答案
p> static
在C ++中有不同的含义。当与变量声明一起使用时,它意味着你正在想的:没有链接的变量,它只是可用的文件中,它被声明或甚至,如果它是一个局部变量,只有一个副本(好方法来声明局部变量,它们应该在它们被使用的地方附近是静态的)。
static
has different meanings in C++ according to where it is used. When used with a variable declaration it means what you were thinking: no linkage for the variable, it is available just to the file in which it is declared or even, if it's a local variable, that there is just one copy of it (good way to declare local variables which are supposed to be static near where they are used).
在类声明中,虽然static意味着与C#中相同的东西,你必须有一个源文件实际声明静态变量(或在方法中声明为 static
),这是因为 static
变量必须在某处保留空间。这可以通过多种方式实现,例如:
In a class declaration though static means the same thing as in C# with the difference that you must have a source file which actually declares the static variable (or declare it as static
inside a method), this because this static
variable must be have the space reserved somewhere. This can be done in multiple ways, for example:
static MyClass& instance() {
static MyClass realInstance;
return realInstance;
}
这样你就可以看到使用 static
:一个是告诉实例()
方法是 static
类的命名空间,而不是单个实例),并且一个声明一个静态变量,即使它是一个局部变量,它只有一个副本。
In this way you can see both ways of using static
: one is to tell that the instance()
method is static
(attached to the class namespace, not to a single instance) in the contained class, and one to declare a static variable that will have just one copy of itself even if it's a local variable.
这篇关于什么是C ++中的C#静态实例的等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!