与const更相关 [英] more related to const

查看:83
本文介绍了与const更相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我不知道这个问题的确切主题。

在我的类construcor中,我将一个值读入变量(成员

变量或全局变量)。我需要通过程序& amp;变量保持不变

在调用构造函数

之前,该值是未知的。更确切地说,我需要在整个程序执行期间保持变量

,但是当

声明它时,该值是未知的。如何申报这样的变数?如果变量

在类中或类外部声明为全局变量,则可以。

解决方案

< blockquote>
pa******@gmail.com 写道:


您好,我不知道这个问题的确切主题。

在我的类construcor中,我将一个值读入一个变量(成员

变量或全局变量)。我需要通过程序& amp;变量保持不变

在调用构造函数

之前,该值是未知的。更确切地说,我需要在整个程序执行期间保持变量

,但是当

声明它时,该值是未知的。如何申报这样的变数?如果该变量

在类中或类外部声明为全局变量,则可以。



你需要了解const成员变量。


- Murali Krishna


pa******@gmail.com 写道:


您好,我不知道这个问题的确切主题。

在我的类construcor中,我将一个值读入一个变量(member

变量或全局变量)。我需要通过程序& amp;变量保持不变

在调用构造函数

之前,该值是未知的。更确切地说,我需要在整个程序执行期间保持变量

,但是当

声明它时,该值是未知的。如何申报这样的变数?如果变量

在类中声明或在类外部声明为全局变量,那就没关系。



这样的东西?


struct X

{

const int value;


X(int n): value(n){}

};

-

Ian Collins。


< blockquote> Murali Krishna写道:


>

你需要了解const成员变量。


- Murali Krishna



你是对的。实际上我的印象是const变量

应该在声明时给出值。对于这样一个愚蠢的

问题。


Hello, I dont know exact subject line for this question.
In my class construcor I will read a value into a variable(member
variable or a global variable). I need that variable to be constant
throught the program & that value is not known until the constructor
is invoked. To be more precise I need the variable to remain constant
throughout the program execution but the value is not known while
declaring it. How to declare such varibles?. It is ok if that variable
is declared inside the class or outside the class as global varible.

解决方案


pa******@gmail.com wrote:

Hello, I dont know exact subject line for this question.
In my class construcor I will read a value into a variable(member
variable or a global variable). I need that variable to be constant
throught the program & that value is not known until the constructor
is invoked. To be more precise I need the variable to remain constant
throughout the program execution but the value is not known while
declaring it. How to declare such varibles?. It is ok if that variable
is declared inside the class or outside the class as global varible.

you need to know about const member variables.

-- Murali Krishna


pa******@gmail.com wrote:

Hello, I dont know exact subject line for this question.
In my class construcor I will read a value into a variable(member
variable or a global variable). I need that variable to be constant
throught the program & that value is not known until the constructor
is invoked. To be more precise I need the variable to remain constant
throughout the program execution but the value is not known while
declaring it. How to declare such varibles?. It is ok if that variable
is declared inside the class or outside the class as global varible.

Something like this?

struct X
{
const int value;

X( int n ) : value(n) {}
};
--
Ian Collins.


Murali Krishna wrote:

>
you need to know about const member variables.

-- Murali Krishna

You are right. Actually I was under the impression that const variables
should be given value at the time of declaration.Sorry for such a silly
question.


这篇关于与const更相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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