关于c ++常量的问题? [英] question on c++ constants?

查看:63
本文介绍了关于c ++常量的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我总是很困惑在多个文件中使用常量。


对于全局常量,我从中得到了一些线索
http:// msdn .microsoft.com / zh-CN / libr ... 2d(VS.80).aspx


因此在头文件写入中:const double PI = 3.14;

每次使用它时,都包含头文件。


但名称空间和类中的常量怎么样?


谢谢你的帮助!


水生

解决方案

shuisheng写道:


我总是很困惑在多个文件中使用常量。


对于全局常量,我从
http://msdn.microsoft.com/en-us/libr ... 2d(VS.80).aspx


所以在头文件写中:const double PI = 3.14;

每次使用它时,都包含头文件。


但是如何关于命名空间和类中的常量?



是的,我们也使用它们。有什么问题?


V

-

请在回复时删除资金''A'电子邮件

我没有回复最热门的回复,请不要问


shuisheng写道:
< blockquote class =post_quotes>
亲爱的所有人,


我总是很困惑在多个文件中使用常量。


For全局常量,我从
http://msdn.microsoft.com/en-us/libr...2d(VS.80).aspx


所以在头文件写作中:const double PI = 3.14;

每次使用它时,都包含头文件。


但名称空间和类中的常量怎么样?


感谢您的帮助!


水生




1.最简单类中的常量是使用枚举生成的:

#include&l t; vector>


class SomeClass

{

enum {SIZE = 7};


int array [SIZE];


std :: vector< intintVector;

public:

SomeClass() :intVector(5){}

};


2.关于全局常量和你提到的头文件,

简单的单词:


你可以#include包含全局常量的头文件只在一个

实现中。文件(例如.cpp)和其他实现文件

(例如.cpp),你可以添加声明:

extern const double PI;

==通常避免全局命名空间中的对象。


3.关于命名空间中的常量,你必须在实现中包含定义

文件(例如将其放在头文件中)。

file1.cpp:

Eg pi.h:


名称空间SomeNamespace

{

const double PI = 3.14;

}


在全局命名空间中应该首选而不是常量:


匿名命名空间中的常量(consts / enums)( =本地文件范围

only)和命名空间中的常量(consts / enums)。

关于类,你可以使用静态常量或(通常是

更好)枚举。


更正:


shuisheng写道:


亲爱的所有人,


我总是很困惑在多个文件中使用常量。


对于全局常量,我得到了来自
的一些线索 http: //msdn.microsoft.com/en-us/libr...2d(VS.80).aspx


因此在头文件编写中:const double PI = 3.14;

每次你唱吧,包括头文件。


但名称空间和类中的常量如何?


感谢您的帮助!


水生



1.课堂上最简单的常量是使用枚举:

#include< vector>


class SomeClass

{

enum {SIZE = 7};


int array [SIZE];


std :: vector< intintVector;

public:

SomeClass():intVector (SIZE){}

};


2.关于全局常量和你提到的头文件,

简单的单词:


你可以#include包含全局常量的头文件只在一个

实现中。文件(例如.cpp)和其他实现文件

(例如.cpp),你可以添加声明:

extern const double PI;

==通常避免全局命名空间中的对象。


3.关于命名空间中的常量,你必须在实现中包含定义

文件(例如将其放在头文件中)。

file1.cpp:

Eg pi.h:


名称空间SomeNamespace

{

const double PI = 3.14;

}


在全局命名空间中应该首选而不是常量:


匿名命名空间中的常量(consts / enums)( =本地文件范围

only)和命名空间中的常量(consts / enums)。

关于类,你可以使用静态常量或(通常是

更好)枚举。


Dear All,

I am always confused in using constants in multiple files.

For global constants, I got some clues from
http://msdn.microsoft.com/en-us/libr...2d(VS.80).aspx

So in header file writing: const double PI = 3.14;
Every time using it, include the header file.

But how about constants in namespaces and classes?

Thanks for your help!

Shuisheng

解决方案

shuisheng wrote:

I am always confused in using constants in multiple files.

For global constants, I got some clues from
http://msdn.microsoft.com/en-us/libr...2d(VS.80).aspx

So in header file writing: const double PI = 3.14;
Every time using it, include the header file.

But how about constants in namespaces and classes?

Yes, we use them too. What''s the problem?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


shuisheng wrote:

Dear All,

I am always confused in using constants in multiple files.

For global constants, I got some clues from
http://msdn.microsoft.com/en-us/libr...2d(VS.80).aspx

So in header file writing: const double PI = 3.14;
Every time using it, include the header file.

But how about constants in namespaces and classes?

Thanks for your help!

Shuisheng



1. The easiest constants in classes are made by using enums:
#include <vector>

class SomeClass
{
enum { SIZE= 7 };

int array[SIZE];

std::vector<intintVector;
public:
SomeClass(): intVector(5) {}
};

2. Regarding global constants and the header file you mentioned, in
simple words:

You can #include the header file with the global constant only in one
"implementation" file (e.g. .cpp) and to the rest implementation files
(e.g. .cpp), you can add the declaration:
extern const double PI;
==In general avoid objects in the global namespace.

3. Regarding constants in namespaces, you have to include the definition
in the implementation file (e.g. by placing it in the header file).
file1.cpp:
E.g. pi.h:

namespace SomeNamespace
{
const double PI = 3.14;
}


What should be preferred instead of constants in the global namespace:

Constants (consts/enums) in anonymous namespaces (= local file scope
only), and constants (consts/enums) in named namespaces.
Regarding classes, you can either use static constants or (usually
better) enums.


Corrected:

shuisheng wrote:

Dear All,

I am always confused in using constants in multiple files.

For global constants, I got some clues from
http://msdn.microsoft.com/en-us/libr...2d(VS.80).aspx

So in header file writing: const double PI = 3.14;
Every time using it, include the header file.

But how about constants in namespaces and classes?

Thanks for your help!

Shuisheng


1. The easiest constants in classes are made by using enums:
#include <vector>

class SomeClass
{
enum { SIZE= 7 };

int array[SIZE];

std::vector<intintVector;
public:
SomeClass(): intVector(SIZE) {}
};

2. Regarding global constants and the header file you mentioned, in
simple words:

You can #include the header file with the global constant only in one
"implementation" file (e.g. .cpp) and to the rest implementation files
(e.g. .cpp), you can add the declaration:
extern const double PI;
==In general avoid objects in the global namespace.

3. Regarding constants in namespaces, you have to include the definition
in the implementation file (e.g. by placing it in the header file).
file1.cpp:
E.g. pi.h:

namespace SomeNamespace
{
const double PI = 3.14;
}


What should be preferred instead of constants in the global namespace:

Constants (consts/enums) in anonymous namespaces (= local file scope
only), and constants (consts/enums) in named namespaces.
Regarding classes, you can either use static constants or (usually
better) enums.


这篇关于关于c ++常量的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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