|
|||
|
Hello,
I have been reading my way through C++ Primer and I noticed that the function prototype for one of the functions resembled this class A{ public: void f(const int& a) const; .... }; what is the purpose of that second const following f's parameters? thanks in advance. |
|
|
||||
|
||||
|
|
|
|||
|
On Apr 8, 8:40*pm, Danny Gratzer <danny.grat...@gmail.com> wrote:
> Hello, > I have been reading my way through C++ Primer and I noticed that the > function prototype for one of the functions resembled this > > class A{ > > public: > * * * * * void f(const int& a) const; > ... > > }; > > what is the purpose of that second const following f's parameters? > > thanks in advance. It means that calling the function doesn't alter the object that it is called on. |
|
|||
|
On Sunday, April 8, 2012 2:56:35 PM UTC-5, (unknown) wrote:
> > > "f() const;", defined inside a struct ( i.e. a "class" ) > can't change the variables in the struct. > > I say "struct" because, to my mind, that's what it is, not a "class". > </pre> Is there a proper name for something like that? also can any other qualifiers besides const be used like that? |
|
|||
|
On Sunday, April 8, 2012 2:56:35 PM UTC-5, (unknown) wrote:
> > > "f() const;", defined inside a struct ( i.e. a "class" ) > can't change the variables in the struct. > > I say "struct" because, to my mind, that's what it is, not a "class". > </pre> I see, are there any other modifiers that can be placed in that position besides const? |
|
|||
|
On 4/8/2012 4:04 PM, danny.gratzer@gmail.com wrote:
> On Sunday, April 8, 2012 2:56:35 PM UTC-5, (unknown) wrote: >> >> >> "f() const;", defined inside a struct ( i.e. a"class" ) >> can't change the variables in the struct. >> >> I say"struct" because, to my mind, that's what it is, not a"class". >> </pre> > > Is there a proper name for something like that? also can any other qualifiers besides const be used like that? It's a cv-qualifier. Other possibilities are "volatile" and "const volatile". V -- I do not respond to top-posted replies, please don't ask |
|
|||
|
Victor Bazarov wrote:
> On 4/8/2012 4:04 PM, danny.gratzer@gmail.com wrote: >> On Sunday, April 8, 2012 2:56:35 PM UTC-5, (unknown) wrote: >>> >>> >>> "f() const;", defined inside a struct ( i.e. a"class" ) >>> can't change the variables in the struct. >>> >>> I say"struct" because, to my mind, that's what it is, not a"class". >>> </pre> >> >> Is there a proper name for something like that? also can any other qualifiers besides const be used like that? > > It's a cv-qualifier. Other possibilities are "volatile" and "const > volatile". In C++11, you can also put a ref-qualifier (& or &&) there, so it is even more similar to regular arguments of a function. |
|
|||
|
danny.gratzer@gmail.com wrote:
> I see, are there any other modifiers that can be placed in that position > besides const? Yes, a member function may be declared as const and/or volatile. Just to provide you with some insight, in the C++ programming language member functions may be interpreted as being funcions which have an implicit parameter which takes a pointer to an object of a particular class. So, conceptually, in the following example both Foo::member_do() and function_do(Foo *) would be equivalent: <code> class Foo { public: void member_do() {}; }; void function_do(Foo *) { } </code> With this in mind, when you declare a member function as const, volatile or const volatile, you are declaring how that function accesses the pointer to an object of that class. So, tweaking the previous example, the following would also be equivalent: <code> class Foo { public: void member_do() const {}; void member_do() volatile {}; void member_do() const volatile {}; }; void function_do(const Foo *) { } void function_do(volatile Foo *) { } void function_do(const volatile Foo *) { } </code> Hope this helps, Rui Maciel |
|
|||
|
On 08.04.2012 22:46, Victor Bazarov wrote:
> On 4/8/2012 4:04 PM, danny.gratzer@gmail.com wrote: >> On Sunday, April 8, 2012 2:56:35 PM UTC-5, (unknown) wrote: >>> >>> >>> "f() const;", defined inside a struct ( i.e. >>> a"class" ) >>> can't change the variables in the struct. >>> >>> I say"struct" because, to my mind, that's what it is, >>> not a"class". >>> </pre> >> >> Is there a proper name for something like that? also can any other >> qualifiers besides const be used like that? > > It's a cv-qualifier. Other possibilities are "volatile" and "const > volatile". And, as of C++11, the cv-qualifiers can be followed by ref-qualifiers, "&" and "&&". Which serve a similar purpose, in restricting use of the methods. C++11 §5.5/6 "In a .* expression whose object expression is an rvalue, the program is ill-formed if the second operand is a pointer to member function with ref-qualifier &. In a .* expression whose object expression is an lvalue, the program is ill-formed if the second operand is a pointer to member function with ref-qualifier &&." C++11 §13.3.1/4 "For non-static member functions, the type of the implicit object parameter is — “lvalue reference to cv X” for functions declared without a ref-qualifier or with the & ref-qualifier — “rvalue reference to cv X” for functions declared with the && ref-qualifier" C++11 §13.3.1/5 "For non-static member functions declared without a ref-qualifier, an additional rule applies: — even if the implicit object parameter is not const-qualified, an rvalue can be bound to the parameter as long as in all other respects the argument can be converted to the type of the implicit object parameter." Essentially, the last para means that the ordinary freely-call-methods-on-temporaries rule is disabled for methods with ref-qualifiers. The two first paras then mean that a "&" method can only be called on an rvalue, and that a "&&" method can only be called on an lvalue. Keeping in mind that rvalue and lvalue refer to expressions, not objects, and also modulo the more precise C++11 terminology which I don't master yet. Cheers, - Alf |
|
|||
|
Paul N <gw7rib@aol.com> wrote:
> It means that calling the function doesn't alter the object that it is > called on. Actually it doesn't. What it means is that if you have a const object (or const reference/pointer to such a type) of that class type, you can call said function. *In principle* said function shouldn't modify the object, or else the semantics of constness get broken, but in practice there are situations where it's actually something that can happen, and there's even a keyword to achieve that (namely "mutable"). Anyways, the key here is that you should *always* declare your members functions as 'const' unless they really need to modify the object. That's because it allows calling those functions when the object itself is const. |
|
|||
|
Paul N skrev 2012-04-08 21:42:
> On Apr 8, 8:40 pm, Danny Gratzer<danny.grat...@gmail.com> wrote: >> Hello, >> I have been reading my way through C++ Primer and I noticed that the >> function prototype for one of the functions resembled this >> >> class A{ >> >> public: >> void f(const int& a) const; >> ... >> >> }; >> >> what is the purpose of that second const following f's parameters? >> >> thanks in advance. > > It means that calling the function doesn't alter the object that it is > called on. Which also allows you to call the function for constant objects. Bo Persson |
|
|||
|
On Sunday, April 8, 2012 8:56:35 PM UTC+1, (unknown) wrote:
> > > "f() const;", defined inside a struct ( i.e. a "class" ) > can't change the variables in the struct. > > I say "struct" because, to my mind, that's what it is, not a "class". > </pre> it's a C++ class |
|
|||
|
On Mon, 2012-04-09, Juha Nieminen wrote:
> Paul N <gw7rib@aol.com> wrote: >> It means that calling the function doesn't alter the object that it is >> called on. > > Actually it doesn't. > > What it means is that if you have a const object (or const reference/pointer > to such a type) of that class type, you can call said function. > > *In principle* said function shouldn't modify the object, or else the > semantics of constness get broken, but in practice there are situations > where it's actually something that can happen, and there's even a keyword > to achieve that (namely "mutable"). Shouldn't modify the object /via the 'this' pointer/ to be more precise. This is ok, for example: struct Foo { void bar(Foo& f) const { mess_up(f); } }; Foo foo; foo.bar(foo); // modifies foo But his book should tell him all of this. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
|
|||
|
Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:
> Shouldn't modify the object /via the 'this' pointer/ to be more > precise. As said, the 'mutable' keyword allows modifications of the object pointed by 'this' even if it's const. (While there are other ways to bypass constness, 'mutable' is by far the cleanest and most self-documenting way of doing it. Basically you are telling at a language level that "this member variable can be modified by const methods.") One concrete example of a valid use for 'mutable' is a non-intrusive smart pointer that uses the double-linking strategy (instead of using a reference counter): When such a smart pointer is copied/assigned, the original (which is passed as const reference to the copy constructor or copy assignment operator) is not modified in its behavior, but internally its "linded list" pointers need to be modified, and marking them as 'mutable' is the cleanest way of doing that. (The external behavior of the pointer still retains its constness, so this is a valid situation to use 'mutable'.) |
|
|||
|
On Tue, 2012-04-10, Juha Nieminen wrote:
> Jorgen Grahn <grahn+nntp@snipabacken.se> wrote: >> Shouldn't modify the object /via the 'this' pointer/ to be more >> precise. > > As said, the 'mutable' keyword allows modifications of the object pointed > by 'this' even if it's const. .... Uh, we are talking about different things. I was unclear, but it should have gone roughly: Someone: FOO doesn't modify the object. You: Explained 'mutable' as an exception. Me: Explained that also, the "doesn't modify" doesn't apply to the object itself. > (While there are other ways to bypass constness, > 'mutable' is by far the cleanest and most self-documenting way of doing it. > Basically you are telling at a language level that "this member variable can > be modified by const methods.") Ok, but I was just aiming to explain the concept, not list ways to bypass it. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|