其中一个恒定的特性使得它不多变? [英] which property of a constant makes it not changable?

查看:97
本文介绍了其中一个恒定的特性使得它不多变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我面对采访时,其中一个问题是对我来说非常棘手。
面试官说,如何让不断能够改变它的价值呢?

Today I faced an interview in which one question was very tricky for me. Interviewer said "how to make constant able to change its value?"

我回答说:用指针,我给他看了一个例子:

I replied "using pointer" and I shown him an example :

int main( void )
{

        const int a = 3; 
        int *ptr;
        ptr = (int*)( &a );

        printf( "A=%d\n", a );
        *ptr = 5; 
        printf( "A=%d\n", a );


        return 0;
}

但他说,这是好的。但是,告诉我这是财产进行持续的非多变?同时他还表示,有一个属性,我们可以改变,使不断变化的。

But he said this is fine. But tell me which is property which makes constant non changeable? and he also said that there is one property which we can change and make constant changeable.

有没有像任何财产?它是如何工作的?

Is there any property like that? How does it work?

推荐答案

如果他说,这是好的,那么他错了:试图修改常量对象给出了不确定的行为。在实践中,三种情况之一可能会发生:

If he said this is fine, then he was wrong: trying to modify a constant object gives undefined behaviour. In practice, one of three things might happen:


  • 常数变量的行为就像一个普通对象,你可以看到它的价值变化;

  • 它存储在内存不可写,并且程序访问冲突崩溃;

  • 每个使用它更换为硬codeD值,你看不到它改变。

语言没有定义常量对象的任何运行时的性能;刚刚编译时会检查你不小心修改它们。

The language doesn't define any run-time properties of const objects; just compile-time checks that you don't accidentally modify them.

这篇关于其中一个恒定的特性使得它不多变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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