获取静态const成员的值 [英] getting the value of a static const member

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

问题描述

我可以发誓你可以使用

获得类的静态成员的值这个语法" class :: member"但我的compliler抱怨。除了为它做功能之外,我该怎么办呢?b $ b我想在这里做什么。这不是一个简单的方法吗?哦,我的导师不会让我使用#define,因为他是

a terd。


标题:


class BigPosInt

{

public:

static const int MAX_DIGITS = 30;

// snip .. 。

};


有问题的功能:


无效输入(istream& ins,BigPosInt& targetBpi )

{

int counter = 0;

char one_byte;

int buffer [BigPosInt :: MAX_DIGITS] ;

//剪断..

}


错误:

BigPosInt.cpp: 170:'MAX_DIGITS''未申报(首先使用此功能)


Thanx,

克里斯托弗

I could of sworn you could get the value of a static member of a class using
this syntax "class::member" but my compliler is complaining. How can I do
what I am trying to do here besides making a function for it. Isnt there a
short easy way? Oh and my instructor wont let me use a #define because he is
a terd.

the header:

class BigPosInt
{
public:
static const int MAX_DIGITS = 30;
// snip...
};

the function in question:

void input(istream& ins, BigPosInt& targetBpi)
{
int counter = 0;
char one_byte;
int buffer[BigPosInt::MAX_DIGITS];
// snip..
}

the error:
BigPosInt.cpp:170: `MAX_DIGITS'' undeclared (first use this function)

Thanx,
Christopher

推荐答案

On Sun,14 Sep 2003 06:09:53 GMT in comp.lang.c ++:

On Sun, 14 Sep 2003 06:09:53 GMT in comp.lang.c++ :


class BigPosInt
{
public:
static const int MAX_DIGITS = 30;
// snip ...
};

有问题的函数:

BigPosInt.cpp:170:`MAX_DIGITS''未声明(首先使用此函数)

class BigPosInt
{
public:
static const int MAX_DIGITS = 30;
// snip...
};

the function in question:
BigPosInt.cpp:170: `MAX_DIGITS'' undeclared (first use this function)




您的编译器是错误的...作为一种解决方法来定义它

在课堂宣言之外,看看它是否会以那种方式飞行。


(即)

const int BigPosInt: :MAX_DIGITS = 30;


-

问候......

比尔



Your compiler is wrong.... as a work around define it
outside the class declaration, see if it''ll fly that way.

(I.e.)
const int BigPosInt::MAX_DIGITS = 30;

--
Regards...
Bill


On Sun,2003年9月14日07:48:25 GMT in comp.lang.c ++:

On Sun, 14 Sep 2003 07:48:25 GMT in comp.lang.c++ :

On Sun,2003年9月14日06:09:53 GMT in comp.lang.c ++:

On Sun, 14 Sep 2003 06:09:53 GMT in comp.lang.c++ :


BigPosInt.cpp:170:`MAX_DIGITS''未声明(首先使用此功能)
BigPosInt.cpp:170: `MAX_DIGITS'' undeclared (first use this function)



你的编译器是错误的...作为一个解决方法在类声明之外定义它,看看它是否会以那种方式飞行。



Your compiler is wrong.... as a work around define it
outside the class declaration, see if it''ll fly that way.




其实我错了,只是浏览了标准。

一个静态const数据成员可以在

声明中初始化(整数类型),但如果它使用in

计划它应该仍然在命名空间范围内定义

,定义不得包含初始值。

(9.4.2-4)


所以我读它的方式;


A级{

public:

static const int AINT = 30; //初始化并声明

};

//本例全局命名空间

const int A :: AINT; //定义了吗?


为糟糕的建议道歉......


-

问候......

比尔



Actually I am wrong, just browsed the standard.
A static const data member can be initialized in the
declaration (integral type), but if it used "in" the
program "it shall" still be defined in namespace scope"
and the definition "shall not" contain an initializer.
(9.4.2-4)

so the way I read it ;

class A {
public:
static const int AINT = 30;// initialized and declared
};
// this case global namespace
const int A::AINT; // defined ?

Apologize for the bad advice....


--
Regards...
Bill


" Percy" < Pe的*** @ noemail.given>在消息中写道

新闻:MP ************************ @ news.prodigy.net ..。
"Percy" <Pe***@noemail.given> wrote in message
news:MP************************@news.prodigy.net.. .
On Sun,2003年9月14日07:48:25 GMT in comp.lang.c ++:

其实我错了,只是浏览了标准。
静态const数据成员可以在
声明(整数类型)中初始化,但如果它使用在...中
程序它应该仍然在命名空间范围内定义
,定义不得包含一个初始化器。
(9.4.2-4)
On Sun, 14 Sep 2003 07:48:25 GMT in comp.lang.c++ :

Actually I am wrong, just browsed the standard.
A static const data member can be initialized in the
declaration (integral type), but if it used "in" the
program "it shall" still be defined in namespace scope"
and the definition "shall not" contain an initializer.
(9.4.2-4)




如果我理解正确,那么成为这样的成员是没有意义的

公开。非常欢迎您将初始化程序放在类

定义中。只是不要在程序中使用它。


我认为这是Stroustrup

不同意的标准的少数添加之一,它难怪。


DW



If I understand this correctly, it is pointless to make such a member
public. You are quite welcome to place the initializer in the class
definition. Just don''t use it in the program.

I thought this was one of the few additions to the standard that Stroustrup
disagreed with, and it''s no wonder.

DW


这篇关于获取静态const成员的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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