C / C ++改变const的值 [英] C/C++ changing the value of a const

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

问题描述

我有一篇文章,但我失去了。它显示和描述了几个C / C ++的技巧,人们应该小心。其中一个有兴趣我,但现在我想复制它,我不能把它编译。<​​/ p>

这个概念是可以改变意外的C / C ++中的 const 的值



这是这样的:

  const int a = 3; //我保证我不会改变
const int * ptr_to_a =& a; //我仍然保证我不会改变一个
int * ptr;
ptr = ptr_to_a;

(* ptr)= 5; //我是骗子a现在是5

我想向朋友展示这个,但现在我错过了一步。



ATM我得到从'const int *'到'int *'的无效转换。 / strong>但是当我阅读我尝试的文章,它工作得很好。

解决方案

你需要抛弃的constness: / p>

  linux〜$ cat constTest.c 
#include< stdio.h>


void modA(int * x)
{
* x = 7;
}


int main(void)
{

const int a = 3; //我promisse我不会改变一个
int * ptr;
ptr =(int *)(& a);

printf(A =%d\\\
,a);
* ptr = 5; //我是骗子,现在是5
printf(A =%d\\\
,a);

*((int *)(& a))= 6;
printf(A =%d\\\
,a);

modA((int *)(& a));
printf(A =%d\\\
,a);

return 0;
}
linux〜$ gcc constTest.c -o constTest
linux〜$ ./constTest
A = 3
A = 5
A = 6
A = 7
linux〜$ g ++ constTest.c -o constTest
linux〜$ ./constTest
A = 3
A = 3
= 3
A = 3

同样的答案在g ++ 4.1中不起作用。 2

  linux〜$ cat constTest2.cpp 
#include< iostream>
using namespace std;
int main(void)
{
const int a = 3; //我promisse我不会改变一个
int * ptr;
ptr = const_cast< int *>(& a);

cout<< A =< a<< endl;
* ptr = 5; // I'm a liar,a is now 5
cout< A =< a<< endl;

return 0;
}
linux〜$ g ++ constTest2.cpp -o constTest2
linux〜$ ./constTest2
A = 3
A = 3
linux〜$

btw ..这是不推荐的...我发现g ++不允许这种情况发生..因此可能是您遇到的问题。


I had an article, but I lost it. It showed and described a couple of C/C++ tricks that people should be careful. One of them interested me but now that I am trying to replicate it I'm not being able to put it to compile.

The concept was that it is possible to change by accident the value of a const in C/C++

It was something like this:

const int a = 3;          // I promise I won't change a
const int *ptr_to_a = &a; // I still promise I won't change a
int *ptr;
ptr = ptr_to_a;

(*ptr) = 5;               // I'm a liar; a is now 5

I wanted to show this to a friend but now I'm missing a step. Does anyone know what's missing for it to start compiling and working?

ATM I'm getting invalid conversion from 'const int*' to 'int*' but when I read the article I tried and it worked great.

解决方案

you need to cast away the constness:

linux ~ $ cat constTest.c
#include <stdio.h>


void modA( int *x )
{
        *x = 7;
}


int main( void )
{

        const int a = 3; // I promisse i won't change a
        int *ptr;
        ptr = (int*)( &a );

        printf( "A=%d\n", a );
        *ptr = 5; // I'm a liar, a is now 5
        printf( "A=%d\n", a );

        *((int*)(&a)) = 6;
        printf( "A=%d\n", a );

        modA( (int*)( &a ));
        printf( "A=%d\n", a );

        return 0;
}
linux ~ $ gcc constTest.c -o constTest
linux ~ $ ./constTest
A=3
A=5
A=6
A=7
linux ~ $ g++ constTest.c -o constTest
linux ~ $ ./constTest
A=3
A=3
A=3
A=3

also the common answer doesn't work in g++ 4.1.2

linux ~ $ cat constTest2.cpp
#include <iostream>
using namespace std;
int main( void )
{
        const int a = 3; // I promisse i won't change a
        int *ptr;
        ptr = const_cast<int*>( &a );

        cout << "A=" << a << endl;
        *ptr = 5; // I'm a liar, a is now 5
        cout << "A=" << a << endl;

        return 0;
}
linux ~ $ g++ constTest2.cpp -o constTest2
linux ~ $ ./constTest2
A=3
A=3
linux ~ $

btw.. this is never recommended... I found that g++ doesn't allow this to happen.. so that may be the issue you are experiencing.

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

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