const classes and const members

21 просмотр
Перейти к первому непрочитанному сообщению

Michael Milirud

не прочитано,
8 авг. 1998 г., 03:00:0008.08.1998
I'm having problems with const specifier. If I have a const instance of a
class, does that mean that all of its member variables are automatically
const as well??? That would explain what I'm seeing but also contradict what
I know about const objects (in which afaik you can call only const methods
but nothing said concerning the member variables) Anyone?

Michael

Siemel Naran

не прочитано,
8 авг. 1998 г., 03:00:0008.08.1998

It's kind of complicated. Basically, every variable in your class that
has type "T" has, in a const class, the type "T const".

So "int" in a non-const class becomes "int const" in a const class, which we
note is the same as "const int".

"const int" remains as "const int".

"int *" becomes "int *const". This one is tricky.

"const int *" which is same as "int const *" becomes "int const *const".

"int&" becomes "int const&".


--
----------------------------------
Siemel B. Naran (sbn...@uiuc.edu)
----------------------------------

Michael Milirud

не прочитано,
8 авг. 1998 г., 03:00:0008.08.1998
Siemel Naran wrote in message ...

>It's kind of complicated. Basically, every variable in your class that
>has type "T" has, in a const class, the type "T const".
Tell me about it ;(

>So "int" in a non-const class becomes "int const" in a const class, which
we
>note is the same as "const int".
>
>"const int" remains as "const int".
>
>"int *" becomes "int *const". This one is tricky.
>
>"const int *" which is same as "int const *" becomes "int const *const".
>
>"int&" becomes "int const&".

Where is this info located inside Stroustrup's book??? I never saw this and
I'm sure I would have noticed... In any case, as I posted later on my
problem is even more localized. I get a compiler error with a non-const
class and only const method! Now is this also some kind of hidden rule or
what?

Michael

chu...@itls.com

не прочитано,
10 авг. 1998 г., 03:00:0010.08.1998
Michael,

If you have a const object instance, that means the STATE of this instance
cannot be changed after it is created. That's why you cannot change the value
of its member attributes once they are initialized. const member methods of
an object promise they will not change the state so they are allowed to be
called.

Hope this explanation help! Let me know if you still have question about this!


Ken Chung
chu...@itls.com


In article <vVMy1.11826$97.87...@news.rdc1.on.wave.home.com>,


"Michael Milirud" <NOmmi...@shaw.wave.caSPAM> wrote:
> I'm having problems with const specifier. If I have a const instance of a
> class, does that mean that all of its member variables are automatically
> const as well??? That would explain what I'm seeing but also contradict what
> I know about const objects (in which afaik you can call only const methods
> but nothing said concerning the member variables) Anyone?
>

> Michael
>
>

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Jim Cobban

не прочитано,
10 авг. 1998 г., 03:00:0010.08.1998
In article <73Oy1.12046$97.87...@news.rdc1.on.wave.home.com>,

Section 3.9.3 paragraph 3 of the draft standard specifies:

"Each non-function, non-static, non-mutable member of a const-qualified class
object is const-qualified."

Somebody borrowed my copy of Stroustrup or I would point out where it is
discussed. Just look under const in the index and read ALL of the
referenced pages, I am sure you will find your explanation.

const means const.
--
Jim Cobban | jco...@nortel.ca | Phone: (613) 763-8013
Nortel (MCS) | | FAX: (613) 763-5199

Ответить всем
Написать сообщение автору
Переслать
0 новых сообщений