使用静态变量和模板 [英] Using static variable along with templates

查看:308
本文介绍了使用静态变量和模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类在这样的头文件中定义。这里我定义了一个静态变量:

I have a template class defined in a header file like this. Here I have defined a static variable as well:

#ifndef TEST1_H_
#define TEST1_H_

void f1();

static int count;

template <class T>
class MyClass
{
public:

    void f()
    {
    	++count;
    }


};

#endif

我已经定义了main cpp文件像这样:

And I have defined main() function in a different cpp file like this:

int main(int argc, char* argv[])
{
    MyClass<int> a;
    a.f();
    f1();

    cout<<"Main:" << count << "\n";

    return 0;
}

我在不同的cpp文件中实现了函数f1 / p>

I have implemented function f1() in a different cpp file like this:

void f1()
{
    MyClass<int> a;
    a.f();

    cout<<"F1: " <<count <<"\n";
}



当使用VC6编译时,输出为F1:0主:2。这怎么可能?另外,一般来说,如果我想使用静态变量和模板,我该如何处理?

When I compiled this using VC6, I got the output as "F1:0 Main:2". How is this possible? Also, in general how should I handle if I want to use static variables along with templates?

推荐答案

的同一个变量,因为你已经在头文件中声明了一个静态变量。当你以这种方式声明一个全局变量 static 时,你说它是编译单元的本地( .o 文件)。由于您在两个编译单元中包含标题,因此您会获得 count 的两个副本。

You're getting two copies of the same variable because you've declared a static variable in a header file. When you declare a global variable static this way, you're saying it's local to the compilation unit (the .o file). Since you include the header in two compilation units, you get two copies of count.

希望这里是与模板类的每个实例相关联的静态模板成员变量。它看起来像这样:

I think what you really want here is a static template member variable associated with each instance of the template class. It would look like this:

template <class T>
class MyClass
{
    // static member declaration
    static int count;
    ...
};

// static member definition
template<class T> int MyClass<T>::count = 0;

这将为您模板的每个实例化计数。也就是说,你将有 MyClass< int> MyClass< foo> MyClass< bar> 等。 f1()现在看起来像这样:

This will get you a count for each instantiation of your template. That is, you'll have a count for MyClass<int>, MyClass<foo>, MyClass<bar>, etc. f1() would now look like this:

void f1() {
    MyClass<int> a;
    a.f();

    cout<<"F1: " << MyClass<int>::count <<"\n";
}






对于MyClass的所有实例化(无论其模板参数如何),您需要使用全局变量


If you want a count for all instantiations of MyClass (regardless of their template parameters), you do need to use a global variable.

但是,您可能不需要直接使用全局变量,因为您在运行初始化之前使用它的风险。你可以通过一个全局静态方法来返回对你的计数的引用:

However, you probably don't want a global variable directly because you run the risk of using it before it gets initialized. You can get around this by making a global static method that returns a reference to your count:

int& my_count() {
    static int count = 0;
    return count;
}

然后从类中访问它:

void f() {
    ++my_count();
}

这将确保count在被使用之前被初始化,而不管哪个编译单元你访问它。有关详细信息,请参见静态初始化顺序的C ++常见问题

This will ensure that count gets initialized before it's used, regardless of which compilation unit you access it from. See the C++ FAQ on static initialization order for more details.

这篇关于使用静态变量和模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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