question about const

Started by
14 comments, last by amish1234 20 years, 8 months ago
quote:Original post by foofightr
civguy, that''s not the same thing.
It''s not the same thing Wildfire posted, but it''s the thing in which Wildfire tried to come up with a counterargument:
> Is it valid for a const function to return a reference
> to a member variable and then have the reference edited
> outside of the function?
quote:Your member variable ''int a'' is not const, and you do not modify it inside getA(), so all is well.

This works too:
struct T {  T(int& b) : a(b) {}  int& a;  void setA(int& b) const {    a = b; //modified inside  }};int main() {  int x = 4, y = 8;  const T t(x);   //note: t is const.  cout << t.a << '','';  t.setA(y);  cout << t.a;} 

Ignore the fact it''s now setA, not getA
quote:Also it doesn''t matter that t is const, because you are not [directly] modifying it, you are using an accessor function.
That''s not really the trick in the example, as shown above. I''m not actually modifying ''t''. I''m modifying the memory where t.a points to. At the end of the example, x == y == 8.
Advertisement
civguy, that's a neat trick.
I would've bet that wouldn't work.
I assume the entire reason is because 'a' is a reference to an integer 'x' that is not a member variable and which existed before the constant class instance 'T' existed?

(Edited last sentence for clarity.)

[edited by - SpaceRogue on August 8, 2003 5:42:25 PM]
struct T {
int* p;
int& r
void setP(int* _p) const { p = _p; } // Illegal
void setR(int& _r) const { r = _r; } // Legal???
};

I think it's kinda stupid. If you consider references are "almost like" pointers, then you really are modifying what the reference points to (analagous to changing the value of the pointer variable). Thus, I feel it shouldn't be allowed.

My philosophy on the matter is that a member variable's VALUE shall not be allowed to change inside a const member function. But a reference variable's VALUE is tricky to define: Is the value of a reference 1) the value of what it points to (i.e. the integer value) or 2) what it points to (i.e the integer variable). I guess C++'s answer is the first, but even if that's the case, if you are changing what a reference points to you are MORE THAN LIKELY changing both (i.e. the variable and the value).

I feel it shouldn't be allowed, but that's just my two cents...does this buy you anything?

Regards,
Jeff

[edited by - rypyr on August 8, 2003 6:02:58 PM]
p = _p may be illegal, but *p = *_p isn't even if p and _p are const pointers (not pointers to const!).

I suppose this happens with references because what is commonly called a constant reference is really a reference to constant data. Something like int& const r which is normally meaningless, seems to be how a reference member of a const object is treated.

It seems as if when you declare a constant object of a class, members get a const tacked on the end:

int i becomes int const i
int* p becomes int* const p // *p can still be modified.
int& r becomes int& const r // r can still be modified.

Weird.

[edited by - SpaceRogue on August 8, 2003 6:15:48 PM]
A reference is always associated with only one object. You can''t make it point to another object. You can''t do:

int& r = new int(5);
r = new int(7);

however you can do:

int b = 6;
int &r = b;// b&r are same
r++;
cout << b << endl;

which will print 7 thru b object.

if you do:

const int &r = b;
r++;// can''t because it''s constant

but you can do:

b++;// b isn''t constant

Interestingly you can''t do:

int &r = 7;

so you must write it as:

const int &r = 7;
r++;// error, r is constant

There are whole bunch of other C++ rules that make the lang. exhaustive.
civguy: Yes, it is surprising at first, but if you give it a little thought you will notice that it doesn't contradict my rule in any way.

If a member function is const you cannot modify any of the classes member-variables (unless defined as 'mutable'), and you must not return them in a way that would allow the user of your class to modify them.

However: your 'int& a' is a reference, and can as such not be modified (the refernece, not the date being refered to). And as long as 'a' is pointing to data outside your class (which is not const) you are not violating said rule.

You could say 'int& a' is the same as 'int* const a' (if you disregared the . vs -> syntax).

Edit: const int* / int* const confusion... damn syntax

Edit2:

However, this works too... which it should not. Guess it's too sneaky for the compiler to notice.
struct T {  int&   a;  int    c;         T     (void) : a(c) {};  int&   getA  (void) const;};int& T::getA(void) const{   return(a);}


[edited by - Wildfire on August 9, 2003 10:52:13 AM]
How do I set my laser printer on stun?

This topic is closed to new replies.

Advertisement