在类中定义静态const int [英] Defining static const int in class

查看:75
本文介绍了在类中定义静态const int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这段代码片段中定义静态const int有一点问题

Hi, I have a little problem in defining static const int in this code snippet

#include <iostream>

class foo
{
public :
    static const int f = 5; 
};

void bar(const int& b)
{

}

int main()
{
    bar(foo::f); //undefined reference to foo::f
}



我找到了一个简单的解决方案


I have found a simple solution

#include <iostream>

class foo
{
public :
    enum {f = 5};
};

void bar(const int& b)
{

}

int main()
{
    bar(foo::f); //works fine !!
}



为什么第一个代码不起作用而第二个代码确实有效?我需要对场景背后发生的事情做一些解释。



谢谢,Sam。


why the first code does not work and the second one does work ? I need some explanation about what happens behind the scene, please.

Thanks, Sam.

推荐答案

这里没什么了不起的。一个是允许语法一个不是。对于const成员变量,必须在类中声明并在外部定义。 http://www-01.ibm。 com / support / knowledgecenter / SSLTBW_1.12.0 / com.ibm.zos.r12.cbclx01 / cplr038.htm [ ^ ]



所以:

Nothing magic here. One is permitted syntax one isn't. For const member variables must be declared in class and defined outside. http://www-01.ibm.com/support/knowledgecenter/SSLTBW_1.12.0/com.ibm.zos.r12.cbclx01/cplr038.htm[^]

So:
class foo
{
public :
    static const float f;
};

const float foo:f = 5.0f;




$ c $ b c ++ 11你能做到:





in c++11 you could do:

class foo
{
public :
    static constexpr float f = 5.0f;
};





但是有些C ++编译器允许省略int的外部定义但是建议如果有的话你打算稍后拿这个地址。这是因为编译器可能会删除变量并保留该值。

请参见此处: http://stackoverflow.com/questions/1312241/using-a-static-const-int-in-a-struct-class [ ^ ]



这是你发现的。你没有提到你的编译器,但这在这里至关重要。



如需进一步说明,请看这里:http://stackoverflow.com/questions/11300652/static-data-member-initialization [ ^ ]



引用

另一方面,静态成员变量不包含在类的实例中,它独立于任何单个实例而存在,并且从程序的开头就存在于固定地址。为了一个静态成员变量(或任何其他全局对象)获取一个唯一的地址,链接器必须在一个目标文件中只看到一个静态变量的定义,并为其分配一个地址。



因为静态变量只需要在一个目标文件中只有一个定义,所以它不会产生传感se允许在类中提供该定义,因为类定义通常存在于头文件中并包含在多个目标文件中。因此,尽管您可以在类中提供初始化程序,但仍需要在某处定义静态数据成员



内存中不存在枚举,它们完全由编译器:

http://stackoverflow.com/ questions / 1206579 / memory-location-of-enum-value-in-c [ ^ ]



However some C++ compilers allow the omission of an external definition for int but it is advised to have one if you intend taking the address later. This is because the compiler may remove the variable and just keep the value.
See here: http://stackoverflow.com/questions/1312241/using-a-static-const-int-in-a-struct-class[^]

This is what you discovered. You did not mention your compiler but that is crucial here.

For further explanation look here:http://stackoverflow.com/questions/11300652/static-data-member-initialization[^]

Quote
"On the other hand, a static member variable is not contained within an instance of the class, it exists independently of any single instance and exists from the start of the program, at a fixed address. In order for a static member variable (or any other global object) to get a unique address the linker must see exactly one definition of the static variable, in exactly one object file, and assign it an address.

Because a static variable needs exactly one definition in exactly one object file, it doesn't make sense to allow that definition to be provided in the class, since class definitions typically exist in header files and are included in multiple object files. So although you can provide an initializer in the class, you still need to define the static data member somewhere"

Enums do not exist in memory they are entirely handled by the compiler:
http://stackoverflow.com/questions/1206579/memory-location-of-enum-value-in-c[^]


它是属于类的变量而不是对象(实例)

静态变量在执行开始时只初始化一次。这些变量将在任何实例变量初始化之前首先初始化

所有人共享的单个副本类的实例

静态变量可以通过类名直接访问,不需要任何对象!



静态变量是类级变量,如果声明的类不能编译,它们不能在方法内声明。



class foo

{

public:

static const int f;

};

const int foo :: f = 5 ;



void bar(const int& b)

{



}



int main()

{

bar(foo :: f);

}



工作正常,编译无错误
It is a variable which belongs to the class and not to object(instance)
Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
A single copy to be shared by all instances of the class
A static variable can be accessed directly by the class name and doesn’t need any object!

Static variable are class level variables, they can't be declared inside a method, if declared class will not compile.

class foo
{
public :
static const int f;
};
const int foo::f = 5;

void bar(const int& b)
{

}

int main()
{
bar(foo::f);
}

Works finely and compiles without error


这篇关于在类中定义静态const int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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