C/C++ changing the value of a const

后端 未结 18 1695
醉梦人生
醉梦人生 2020-11-28 07:28

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 repli

相关标签:
18条回答
  • 2020-11-28 07:49

    Did you try this?

    ptr = const_cast<int *>(ptr_to_a);
    

    That should help it compile but it's not really by accident due to the cast.

    0 讨论(0)
  • 2020-11-28 07:52

    Final Solution: it will change const variable a value;

    cont int a = 10;
    *(int*)&a= 5; // now a prints 5
    // works fine.
    
    0 讨论(0)
  • 2020-11-28 07:54

    Back in the mists of time, we paleo-programmers used FORTRAN. FORTRAN passed all its parameters by reference, and didn't do any typechecking. This meant it was quite easy to accidentally change the value of even a literal constant. You could pass "3" to a SUBROUTINE, and it would come back changed, and so every time from then on where your code had a "3", it would actually act like a different value. Let me tell you, those were hard bugs to find and fix.

    0 讨论(0)
  • 2020-11-28 07:56

    I was looking on how to convert between consts and I found this one http://www.possibility.com/Cpp/const.html maybe it can be useful to someone. :)

    0 讨论(0)
  • 2020-11-28 07:57

    Just a guess, but a common question is why one can't convert an int** to a const int**, which at first appears to be reasonable (after all, you're just adding a const, which is normally ok). The reason is that if you could do this, you could accidentally modify a const object:

    const int x = 3;
    int *px;
    const int **ppx = &px;  // ERROR: conversion from 'int**' to 'const int**'
    *ppx = &x;  // ok, assigning 'const int*' to 'const int*'
    *px = 4;    // oops, just modified a const object
    

    It's a very non-intuitive result, but the only way to make sure that you can't modify a const object in this case (note how there are no typecasts) is to make line 3 an error.

    You're only allowed to add const without a cast at the FIRST level of indirection:

    int * const *ppx = &px;  // this is ok
    *ppx = &x;               // but now this is an error because *ppx is 'const'
    

    In C++, it is impossible to modify a const object without using a typecast of some sort. You'll have to use either a C-style cast or a C++-style const_cast to remove the const-ness. Any other attempt to do so will result in a compiler error somewhere.

    0 讨论(0)
  • 2020-11-28 07:58

    I have tested the code below and it successfully changes the constant member variables.

    #include <iostream>
    
    class A
    {
        private:
            int * pc1;  // These must stay on the top of the constant member variables.
            int * pc2;  // Because, they must be initialized first
            int * pc3;  // in the constructor initialization list.
        public:
            A() : c1(0), c2(0), c3(0), v1(0), v2(0), v3(0) {}
            A(const A & other)
                :   pc1 (const_cast<int*>(&other.c1)),
                    pc2 (const_cast<int*>(&other.c2)),
                    pc3 (const_cast<int*>(&other.c3)),
                    c1  (*pc1),
                    c2  (*pc2),
                    c3  (*pc3),
                    v1  (other.v1),
                    v2  (other.v2),
                    v3  (other.v3)
            {
            }
            A(int c11, int c22, int c33, int v11, int v22, int v33) : c1(c11), c2(c22), c3(c33), v1(v11), v2(v22), v3(v33)
            {
            }
            const A & operator=(const A & Rhs)
            {
                pc1     =  const_cast<int*>(&c1);
                pc2     =  const_cast<int*>(&c2),
                pc3     =  const_cast<int*>(&c3),
                *pc1    = *const_cast<int*>(&Rhs.c1);
                *pc2    = *const_cast<int*>(&Rhs.c2);
                *pc3    = *const_cast<int*>(&Rhs.c3);
                v1      = Rhs.v1;
                v2      = Rhs.v2;
                v3      = Rhs.v3;
                return *this;
            }
            const int c1;
            const int c2;
            const int c3;
            int v1;
            int v2;
            int v3;
    };
    
    std::wostream & operator<<(std::wostream & os, const A & a)
    {
        os << a.c1 << '\t' << a.c2 << '\t' << a.c3 << '\t' << a.v1 << '\t' << a.v2 << '\t' << a.v3 << std::endl;
        return os;
    }
    
    int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
    {
        A ObjA(10, 20, 30, 11, 22, 33);
        A ObjB(40, 50, 60, 44, 55, 66);
        A ObjC(70, 80, 90, 77, 88, 99);
        A ObjD(ObjA);
        ObjB = ObjC;
        std::wcout << ObjA << ObjB << ObjC << ObjD;
    
        system("pause");
        return 0;
    }
    

    The console output is:

    10      20      30      11      22      33
    70      80      90      77      88      99
    70      80      90      77      88      99
    10      20      30      11      22      33
    Press any key to continue . . .
    

    Here, the handicap is, you have to define as many pointers as number of constant member variables you have.

    0 讨论(0)
提交回复
热议问题