简体   繁体   中英

Constant for everyone but the class, is there such a thing?

I've always wondered is there a way to have a class member without using getters that can only be modified by it's class?

What I'm thinking of is something like this.

class A 
{
    public:
        crazyconst int x;

        void doStuff()
        {
             // Gettin' stuff done
             x = someValue; // OK
        }
};

int main(int argc, char** argv)
{
     A a;
     a.x = 4; // ERROR
}

So it's visible, but read-only for everyone beside its class.

Your class could have a public const reference to a private, non- const data member.

Edit: However, I should point out that doing this would prevent you from using the compiler-generated copy constructor and copy assignment operator.

The answer is no you can't do this without a getter of some sort. However, you can make the getter reusable and you can make the simple syntax of a field work (mostly), without parentheses.

(C++11 required)

template<typename Friend, typename FieldType>
class crazyconst
{
    FieldType value;
    friend Friend;
    FieldType& operator=(const FieldType& newValue) { return value = newValue; }
public:
    operator FieldType(void) const { return value; }
    FieldType operator()(void) const { return value; }
};

class A
{
public:
    crazyconst<A, int> x;

    void doStuff()
    {
        // Gettin' stuff done
        x = 5; // OK
    }
};

int main(int argc, char** argv)
{
    A a;
    int b = a.x;
    int c = a.x(); // also works
}

C++03 version: http://ideone.com/8T1Po

But beware, this will compile but not work as expected:

const int& the_x = a.x;
a.doStuff();
std::cout << the_x;

OTOH, this should be fine:

const auto& ref_x = a.x;
a.doStuff();
std::cout << ref_x;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM