English 中文(简体)
C++的图书馆和输出静态变量
原标题:Libraries and exporting static variables in C++
  • 时间:2012-05-25 14:53:21
  •  标签:
  • c++

想象我有这样的图书馆:

图书馆h

   class DLLEXPORT LibraryClass
    {
    private:
      int _id;
      static int _last_id;
    public:
      LibraryClass();
      bool operator == (const LibraryClass t)
      {return _id == t._id;}
    };

图书馆.cpp

#include "图书馆h"

int LibraryClass::_last_id = 0;

LibraryClass::LibraryClass()
_id(_last_id)
{
++_last_id;
}

是否正确? 我在视觉工作室得到C4835警告,但似乎有效。有人知道它如何适用于其他编译者(i m interested to linux gcc and mac gcc)吗?是否有另一种“有效的”执行模式?

最佳回答

您的语法是正常的, 这不应该引起您的代码中出现任何问题; 我不认为您在编译 UNIX/ MAC 系统时会看到任何警告 。 除了您正在做 DLL 导出, 且导出为窗口外 。 我还记得您正在看到管理过的 C++ 的结果 。

来自 MSDN 的 MSDN :

variable : the initializer for exported data will not be run until managed code is first executed in the host assembly

When accessing data between managed components, it is recommended that you not use native C++ import and export mechanisms. Instead, declare your data members inside a managed type and reference the metadata with #using in the client. For more information, see #using Directive (C/C++).

您的静态数据成员在编译为 unix 时将进入程序初始化测试段。 它将保证被初始化为您执行前提供的价值, 因此它将从 0 开始, 在被引用时, 它将完全用于您的构建器 。

问题回答

">document 似乎只有在尝试在初始化阶段使用该值时才会出现问题(在初始化阶段(在 main 之前)。显然,这是一个与管理模式 有关的问题。

C++标准对此非常清楚: 在任何动态初始化之前,它将首先静态初始化为 0 ,这意味着任何符合要求的汇编者都会产生出你所期望的有效代码。

因此,它将与 gcc 和 cingg (不管操作系统如何)合作。

对于视觉演播室,我不太确定(不太清楚)这个问题是否会系统地出现,或者只有在您需要有一定旗帜的 < em> 管理模式 < / em > 时才会出现。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签