SpaceFace
SpaceFace

Reputation: 1552

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.

Upvotes: 2

Views: 98

Answers (2)

jamesdlin
jamesdlin

Reputation: 90015

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.

Upvotes: 6

Ben Voigt
Ben Voigt

Reputation: 283684

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;

Upvotes: 4

Related Questions