Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2012, 07:40 PM
Danny Gratzer
Guest
 
Posts: n/a
Default Question on Basic Function Syntax

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.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-08-2012, 07:42 PM
Paul N
Guest
 
Posts: n/a
Default Re: Question on Basic Function Syntax

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.
Reply With Quote
  #3 (permalink)  
Old 04-08-2012, 07:56 PM
@Jeff-Relf.Me
Guest
 
Posts: n/a
Default "f() const;", defined inside a struct ( i.e. a "class" ). 

<PRE>
"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".
Reply With Quote
  #4 (permalink)  
Old 04-08-2012, 08:04 PM
danny.gratzer@gmail.com
Guest
 
Posts: n/a
Default Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

On Sunday, April 8, 2012 2:56:35 PM UTC-5, (unknown) wrote:
> 
>
> &quot;f() const;&quot;, defined inside a struct ( i.e. a &quot;class&quot; )
> can't change the variables in the struct.
>
> I say &quot;struct&quot; because, to my mind, that's what it is, not a &quot;class&quot;.
> </pre>


Is there a proper name for something like that? also can any other qualifiers besides const be used like that?
Reply With Quote
  #5 (permalink)  
Old 04-08-2012, 08:11 PM
danny.gratzer@gmail.com
Guest
 
Posts: n/a
Default Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

On Sunday, April 8, 2012 2:56:35 PM UTC-5, (unknown) wrote:
> 
>
> &quot;f() const;&quot;, defined inside a struct ( i.e. a &quot;class&quot; )
> can't change the variables in the struct.
>
> I say &quot;struct&quot; because, to my mind, that's what it is, not a &quot;class&quot;.
> </pre>


I see, are there any other modifiers that can be placed in that position besides const?
Reply With Quote
  #6 (permalink)  
Old 04-08-2012, 08:46 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

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:
>> 
>>
>> &quot;f() const;&quot;, defined inside a struct ( i.e. a&quot;class&quot; )
>> can't change the variables in the struct.
>>
>> I say&quot;struct&quot; because, to my mind, that's what it is, not a&quot;class&quot;.
>> </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
Reply With Quote
  #7 (permalink)  
Old 04-08-2012, 09:03 PM
Marc
Guest
 
Posts: n/a
Default Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

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:
>>> 
>>>
>>> &quot;f() const;&quot;, defined inside a struct ( i.e. a&quot;class&quot; )
>>> can't change the variables in the struct.
>>>
>>> I say&quot;struct&quot; because, to my mind, that's what it is, not a&quot;class&quot;.
>>> </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.
Reply With Quote
  #8 (permalink)  
Old 04-08-2012, 09:22 PM
Rui Maciel
Guest
 
Posts: n/a
Default Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

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
Reply With Quote
  #9 (permalink)  
Old 04-08-2012, 09:35 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

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:
>>> 
>>>
>>> &quot;f() const;&quot;, defined inside a struct ( i.e.
>>> a&quot;class&quot; )
>>> can't change the variables in the struct.
>>>
>>> I say&quot;struct&quot; because, to my mind, that's what it is,
>>> not a&quot;class&quot;.
>>> </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
Reply With Quote
  #10 (permalink)  
Old 04-09-2012, 08:03 AM
Juha Nieminen
Guest
 
Posts: n/a
Default Re: Question on Basic Function Syntax

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.
Reply With Quote
  #11 (permalink)  
Old 04-09-2012, 12:22 PM
Bo Persson
Guest
 
Posts: n/a
Default Re: Question on Basic Function Syntax

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

Reply With Quote
  #12 (permalink)  
Old 04-10-2012, 11:05 AM
nick_keighley_nospam@hotmail.com
Guest
 
Posts: n/a
Default Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

On Sunday, April 8, 2012 8:56:35 PM UTC+1, (unknown) wrote:
> 
>
> &quot;f() const;&quot;, defined inside a struct ( i.e. a &quot;class&quot; )
> can't change the variables in the struct.
>
> I say &quot;struct&quot; because, to my mind, that's what it is, not a &quot;class&quot;.
> </pre>


it's a C++ class
Reply With Quote
  #13 (permalink)  
Old 04-10-2012, 02:12 PM
Jorgen Grahn
Guest
 
Posts: n/a
Default Re: Question on Basic Function Syntax

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 .
Reply With Quote
  #14 (permalink)  
Old 04-10-2012, 02:30 PM
Juha Nieminen
Guest
 
Posts: n/a
Default Re: Question on Basic Function Syntax

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'.)
Reply With Quote
  #15 (permalink)  
Old 04-10-2012, 09:57 PM
Jorgen Grahn
Guest
 
Posts: n/a
Default Re: Question on Basic Function Syntax

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 .
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:20 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.