Topic: const int const num; not pointer !! what does it mean?
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/01/12 Raw View
alvin <alvin@cyberramp.net> writes:
> Q. What do they mean?
> Situation 1:
>
> const void method1();
it means...
nothing (IMO incorrect code)
If you change void to int:
const int method1();
nothing
> Situation 2:
>
> const int const number;
nothing (incorrect code)
> Situation 3:
>
> int method1() const;
I assume method1 () is a member function; it's applied to
an object:
obj.method1();
It simply means that the object is a const parameter, so:
const Type obj = ...;
obj.method1();
works. 'this' is of type 'const Type*' instead of 'Type*'
inside method1 ().
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/01/12 Raw View
alvin writes:
> const void method1();
In this case, `const' does not affect the method at all, except that
it changes the method type. Since method that returns a const object
is different from a method that returns a non-const object, (not)
returning const void is different from (not) returning void. The
pointer type to this method is not implicitly convertible to type
`void (CL::*)()', where CL is the class that declares this method.
> int const number;
> const int const number;
The first line is invalid in namespace scope, because const-qualified
objects declared in namespace scope must have an initializer. The
second definition is invalid because it duplicates the const
qualifier, and this is only valid if this duplication is caused by
const-qualifying a const-qualified typedef or template argument.
The following valid line defines a constant `number' that evaluates to
3. It can be used wherever the number 3 would be acceptable:
int const number = 3;
> int method1() const;
> Q. What is const doing in this usage?
This usage of const applies to the object pointed to the implicit
`this' passed on to the method. Methods whose declarations do not
indicate that will have `CL *this'; methods that do will have `CL
const *this'. Only non-static methods and pointers thereof can be
qualified like that.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/01/12 Raw View
alvin wrote:
>
> I have been working at understanding 'const' in C++
>
> I ran into these 3 situations and would appreciate
> some assistance.
>
> Situation 1:
>
> const void method1();
>
> Q. What does const mean when
> applied to a void return value?
AFIK, it has no meaning. It is permitted, to simplify the writing of
templates; the 'void' might have been the value of 'T' from a 'template
<T>'.
>
>
> Situation 2:
>
> int const number;
> const int const number;
>
> Notice that number is not a pointer.
>
> Q. What do they mean?
They both mean only that 'number' should not be changed; in the absence
of an initialization expression, that would be pretty pointless. I'd
like a warning in the second case, but I think it's legal.
>
>
> Situation 3:
>
> int method1() const;
>
> Q. What is const doing in this usage?
This usage is meaningful only for member functions. The name of this
funciton implies that it is a member function. What 'const' means here
is that the object with which this member function is associated should
not be changed.
A good way to think about member functions, is to think of them as
taking a single extra argument beyond the visible ones, which is a
pointer to the object they are called on. In many systems, that is
exactly how they are implemented. Since the extra argument is implicit,
this syntax is the only way of applying cv-qualifiers to it.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Ed Staub" <estaub@worldnet.att.net>
Date: 1998/01/12 Raw View
Interesting quiz. I think I get a C.
1. I have no clue. My first guess is that it's a no-op.
2. I'm not sure, but I think the syntax of where to put "const" is fairly
loose. In any case, it's saying that the variable may not be modified after
initialization.
3. This says that the method does not alter the object; it's a read-only
method. (Stroustrup p229).
[Moderator, if you get a better response, feel free to kill this.]
--
-Ed Staub <Ed_Staub@xxxworldnet.att.net>
(remove the xxx from reply address)
alvin wrote in message <01bd1e0d$8f7bb820$48539ecf@default>...
>I have been working at understanding 'const' in C++
>
>I ran into these 3 situations and would appreciate
>some assistance.
>
>Situation 1:
> const void method1();
>
> Q. What does const mean when
> applied to a void return value?
>
>Situation 2:
> int const number;
> const int const number;
>
>Q. What do they mean?
>Situation 3:
> int method1() const;
>Q. What is const doing in this usage?
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: claus@faerber.muc.de (=?ISO-8859-1?Q?Claus_Andr=E9_F=E4rber?=)
Date: 1998/01/13 Raw View
alvin <alvin@cyberramp.net> schrieb:
>
> const void method1();
>
> Q. What does const mean when
> applied to a void return value?
As void is not a value at all, it can't really be mutable or const. =20
However, for symmetry with otther types, declaring const'ness still =20
seems to be allowed although it has no effect.
> int const number;
> const int const number;
>
> Notice that number is not a pointer.
This syntax is invalid. Unless "introduced through the use of typedefs =20
or template type arguments", "redundant cv-qualifiers are prohibited". =20
(quote from WD2.)
However, some compilers still seem to allow it, although a second const =20
has no effect.
> int method1() const;
>
> Q. What is const doing in this usage?
In this case, the method (and it's only valid for methods!) is declared =20
const, which means it doesn't change any member variables (except those =20
declared mutable), so it can be called for const objects of the class.
--=20
Claus Andr=E9 F=E4rber <http://www.muc.de/~cfaerber/> Fax: +49_8061_3361
PGP: ID=3D1024/527CADCD FP=3D12 20 49 F3 E1 04 9E 9E 25 56 69 A5 C6 A0 C=
9 DC
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "alvin" <alvin@cyberramp.net>
Date: 1998/01/10 Raw View
I have been working at understanding 'const' in C++
I ran into these 3 situations and would appreciate
some assistance.
Situation 1:
const void method1();
Q. What does const mean when
applied to a void return value?
Situation 2:
int const number;
const int const number;
Notice that number is not a pointer.
Q. What do they mean?
Situation 3:
int method1() const;
Q. What is const doing in this usage?
Thanks,
alvin b
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]