本文介绍了用C ++编写的const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的const与C中的const之间有什么根本区别?
我知道C中的const并不意味着''常量''

但C ++中的iirc const确实如此。另外,在C中用
,有人认为const意思是只读。

C中也存在古怪与

使用const与指针

对象相关联并且这些奇怪的东西不存在

在C ++中。例如,const char ** foo;


在C中,上面是一个指向类型的指针

"指向char只读的指针"所以

如下以下作业

无法作出:

const char ** foo;

char * * baz;

foo = baz;


但是在C ++中,我想我记得

读取这个有效。


欢迎任何澄清。


-

conrad

What are some of the fundamental differences
between const in C++ and const in C?

I know const in C does not mean ''constant''
but iirc const in C++ does. Additionally,
in C, one thinks of const as meaning ''read-only''.
There also exist oddities in C with
the use of const in association with pointer
objects and that such oddities do not exist
in C++. For example, const char **foo;

In C, the above is a pointer to type
"pointer to char read-only" and so
assignments like the following
cannot be made:
const char **foo;
char **baz;
foo = baz;

But in C++, I think I remember
reading that this works.

Any clarification welcomed.

--
conrad

推荐答案



它在C ++中有所期待,它在C中有点破碎?


在C中,const整数类型不是编译时间常数,而在C ++中它是。

It works as one would expect in C++ and it''s a bit broken in C?

In C, a const integer type isn''t a compile time constant while in C++ it is.



在C中,你可以逃避这个(我不确定编译器是否需要发出诊断所需的
), C ++,这是一个错误。


-

Ian Collins。

In C, you could get away with this (I''m not sure if the compiler is
required to issue a diagnostic), in C++, it is an error.

--
Ian Collins.




不,它在C和C ++中是一样的。你正在阅读的是什么

是错误的还是你记错了。


你不能将char **转换为const char **因为它'这是

不安全。


想象一下:


const char really_const =''?'' ;


void f(const char *& x){

x =& really_const; // valid:assignmetn of const

char * to const char *(reference)。

}


int main() {

char * y;


f(y); //想象编译器允许你这样做(它不是
不应该)。


* y =''!''; //砰!刚修改了一个真的好话


}


[Se

Nope, it''s the same in C and C++. Either what
you are reading is wrong or you misremember it.

You can''t convert from char** to const char** because it''s
not type safe.

Imagine this:

const char really_const = ''?'';

void f(const char*& x) {
x = &really_const; // valid: assignmetn of const
char* to const char*(reference).
}

int main() {
char* y;

f(y); // imagine the compiler let you do this (it
shouldn''t).

*y = ''!''; // BOOM! Just modified a really_const

}

[ Se




没有任何根本的区别,只是一些小的

细节。

There aren''t any fundamental differences, just some small
details.



形式上,规则是相同的:尝试修改const

对象是未定义的行为。您可能想到的差异是:在C ++中,一个const变量初始化为

一个整数常量表达式本身可以用作

积分常数表达式;在C中不是这种情况。

Formally, the rules are the same: attempting to modify a const
object is undefined behavior. The difference you''re probably
thinking about is that in C++, a const variable initialized with
an integral constant expression can itself be used as an
integral constant expression; this is not the case in C.



在C ++中也是如此。

In C++ too.



Nope。


你可能会想到类似的东西:


char const * const * p;

char ** q;

p = q;


这适用于C ++,但不是在C中(至少在C 90中没有)。在这种情况下,

的原因是C90标准的疏忽;

最初都有效,但是有人发现了漏洞Ron

指出,在C90标准化的最后阶段,加上文字

来关闭它。在C90发布之后,它实现了文本实际上限制太多,以及它b / b
也禁止上面的例子,尽管有

这里没有违反const安全。所以C ++

标准的作者重写了规则,允许那些没有引起问题的案件。 (显然,C99的作者并不觉得采用这种改变需要
。)


-

James Kanze(GABI Software)电子邮件:james.ka ... @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,法国,+ 33(0)1 30 23 00 34

Nope.

You''re probably thinking of something like:

char const* const* p ;
char ** q ;
p = q ;

This works in C++, but not in C (at least, not in C 90). The
reason, in this case, was an oversight in the C90 standard;
originally, both worked, but someone spotted the loophole Ron
pointed out, very late in the standardization of C90, and text
was added to close it. After the promulgation of C90, it was
realized that the text was actually too restrictive, that it
also forbid something like my example, above, although there is
no violation of const safety here. So the authors of the C++
standard rewrote the rule to allow the cases which didn''t cause
problems. (Apparently, the authors of C99 didn''t feel it
necessary to adopt this change.)

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于用C ++编写的const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:19