const member and const method
Hello,
today I learnt this:
Code:
class A {
int* _value;
public:
A (int value) : _value (new int(value)) {}
~A() { delete _value; }
void redouble () const { *_value *= 2; } // const method can modified the value pointed by _value
};
And it isn't what I want: is there a way treat this thing, please
Re: const member and const method
Keep a reference instead of a pointer.
Re: const member and const method
Quote:
Originally Posted by
wysota
Keep a reference instead of a pointer.
Why do you suggest a reference? Because a pointer like this isn't good (then I can take a simple int) !?
Code:
int & _value
A (int value = 0) : _value (value) {}
An if I had the sistuation here above (where the constructor cant take nothing) ? here, what I have to assign in the constructor? ('0' for pointers means 'nothing', then for "references" what means "nothing"?)
Re: const member and const method
Quote:
Originally Posted by
mickey
An if I had the sistuation here above (where the constructor cant take nothing) ? here, what I have to assign in the constructor? ('0' for pointers means 'nothing', then for "references" what means "nothing"?)
You can't do that. References don't allow reseating, and thus no late initialization.
If you want to keep using pointers remember that
means a const pointer to a non const object. I.e. you can change the pointee, but not the pointer. If you want a pointer, where you can't change the value, you need to declare it:
But then you can't change the value in a non const method either.
Re: const member and const method
A solution might be to have a wrapper object around the pointer that would only allow accessing the pointed object in a non-const way. Something like a shared data pointer.
Re: const member and const method
I don't understand if this behaviour of C++ (const methods on pointer data, I mean), is right or it's something similar to an accepted bug...
In general if I had a situation like that isn't dangerous to have a method const that isn't really const at all?
Re: const member and const method
The behaviour you observe is correct. Object pointed to by a pointer is not part of the object containing the pointer, thus changing it from a const method of the object is allowed. It's exactly the same as you would change a global (or any other external) object.
Re: const member and const method
Quote:
Originally Posted by
wysota
The behaviour you observe is correct. Object pointed to by a pointer is not part of the object containing the pointer, thus changing it from a const method of the object is allowed. It's exactly the same as you would change a global (or any other external) object.
And are these two statics part of class?
Code:
class A {
static const int _size = 100; //1
static int rom; //2
}
int A::rom = 10;
Re: const member and const method
Part of class - yes. Part of object - no. Thus probably (not sure about it) changing "rom" from a const method is allowed (the other variable is const, so you won't be able to modify it).