J
Jonathan Lee
So I've seen on some web sites a solution for the problem of having a
class member that is public and read-only. Probably you've all seen
this, but basically it makes a reference to a private member, and adds
a const:
class SoundEffect {
public:
SoundEffect() : bitrate(_bitrate) { };
long const & bitrate;
private:
long _bitrate; // Info the user might want, but class doesn't
use.
}
Personally, I don't mind this approach over a getBitrate() member
_supposing_ that the value of "_bitrate" is not essential to the
operation of SoundEffect. i.e., if someone worked around the const and
changed "_bitrate", it wouldn't be that important.
I'm wondering if there are other caveats to this method. To me it
seems friendlier for the user of the class, but maybe I'm overlooking
something.
class member that is public and read-only. Probably you've all seen
this, but basically it makes a reference to a private member, and adds
a const:
class SoundEffect {
public:
SoundEffect() : bitrate(_bitrate) { };
long const & bitrate;
private:
long _bitrate; // Info the user might want, but class doesn't
use.
}
Personally, I don't mind this approach over a getBitrate() member
_supposing_ that the value of "_bitrate" is not essential to the
operation of SoundEffect. i.e., if someone worked around the const and
changed "_bitrate", it wouldn't be that important.
I'm wondering if there are other caveats to this method. To me it
seems friendlier for the user of the class, but maybe I'm overlooking
something.