Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15247 > unrolled thread

Question on Basic Function Syntax

Started byDanny Gratzer <danny.gratzer@gmail.com>
First post2012-04-08 12:40 -0700
Last post2012-04-09 14:22 +0200
Articles 15 — 12 participants

Back to article view | Back to comp.lang.c++


Contents

  Question on Basic Function Syntax Danny Gratzer <danny.gratzer@gmail.com> - 2012-04-08 12:40 -0700
    Re: Question on Basic Function Syntax Paul N <gw7rib@aol.com> - 2012-04-08 12:42 -0700
      "f() const;", defined inside a struct ( i.e. a "class" ).  @Jeff-Relf.Me - 2012-04-08 12:56 -0700
        Re: "f() const;", defined inside a struct ( i.e. a "class" ).  danny.gratzer@gmail.com - 2012-04-08 13:11 -0700
          Re: "f() const;", defined inside a struct ( i.e. a "class" ).  Rui Maciel <rui.maciel@gmail.com> - 2012-04-08 22:22 +0100
        Re: "f() const;", defined inside a struct ( i.e. a "class" ).  danny.gratzer@gmail.com - 2012-04-08 13:04 -0700
          Re: "f() const;", defined inside a struct ( i.e. a "class" ).  Victor Bazarov <v.bazarov@comcast.invalid> - 2012-04-08 16:46 -0400
            Re: "f() const;", defined inside a struct ( i.e. a "class" ).  Marc <marc.glisse@gmail.com> - 2012-04-08 21:03 +0000
            Re: "f() const;", defined inside a struct ( i.e. a "class" ).  "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-04-08 23:35 +0200
        Re: "f() const;", defined inside a struct ( i.e. a "class" ).  nick_keighley_nospam@hotmail.com - 2012-04-10 04:05 -0700
      Re: Question on Basic Function Syntax Juha Nieminen <nospam@thanks.invalid> - 2012-04-09 08:03 +0000
        Re: Question on Basic Function Syntax Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-04-10 14:12 +0000
          Re: Question on Basic Function Syntax Juha Nieminen <nospam@thanks.invalid> - 2012-04-10 14:30 +0000
            Re: Question on Basic Function Syntax Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-04-10 21:57 +0000
      Re: Question on Basic Function Syntax Bo Persson <bop@gmb.dk> - 2012-04-09 14:22 +0200

#15247 — Question on Basic Function Syntax

FromDanny Gratzer <danny.gratzer@gmail.com>
Date2012-04-08 12:40 -0700
SubjectQuestion on Basic Function Syntax
Message-ID<d8d21a09-f8b8-4644-8967-8095ee1c6eca@f27g2000yqc.googlegroups.com>
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.

[toc] | [next] | [standalone]


#15248

FromPaul N <gw7rib@aol.com>
Date2012-04-08 12:42 -0700
Message-ID<e0dfbedf-9308-48b4-ac89-3ce8d63497a1@h12g2000yqi.googlegroups.com>
In reply to#15247
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.

[toc] | [prev] | [next] | [standalone]


#15249 — "f() const;", defined inside a struct ( i.e. a "class" ). 

From@Jeff-Relf.Me
Date2012-04-08 12:56 -0700
Subject"f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<Jeff-Relf.Me@2012_Apr.8^0.56^PM>
In reply to#15248
<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".

[toc] | [prev] | [next] | [standalone]


#15250 — Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

Fromdanny.gratzer@gmail.com
Date2012-04-08 13:11 -0700
SubjectRe: "f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<5402189.2428.1333915876342.JavaMail.geo-discussion-forums@ynbi17>
In reply to#15249
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&#39;t change the variables in the struct.
> 
> I say &quot;struct&quot; because, to my mind, that&#39;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?

[toc] | [prev] | [next] | [standalone]


#15254 — Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

FromRui Maciel <rui.maciel@gmail.com>
Date2012-04-08 22:22 +0100
SubjectRe: "f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<jlsvij$v6i$1@speranza.aioe.org>
In reply to#15250
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

[toc] | [prev] | [next] | [standalone]


#15251 — Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

Fromdanny.gratzer@gmail.com
Date2012-04-08 13:04 -0700
SubjectRe: "f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<26841897.341.1333915449233.JavaMail.geo-discussion-forums@ynpp8>
In reply to#15249
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&#39;t change the variables in the struct.
> 
> I say &quot;struct&quot; because, to my mind, that&#39;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?

[toc] | [prev] | [next] | [standalone]


#15252 — Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2012-04-08 16:46 -0400
SubjectRe: "f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<jlstft$539$1@dont-email.me>
In reply to#15251
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&#39;t change the variables in the struct.
>>
>> I say&quot;struct&quot; because, to my mind, that&#39;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

[toc] | [prev] | [next] | [standalone]


#15253 — Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

FromMarc <marc.glisse@gmail.com>
Date2012-04-08 21:03 +0000
SubjectRe: "f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<jlsuft$sdo$1@news-rocq.inria.fr>
In reply to#15252
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&#39;t change the variables in the struct.
>>>
>>> I say&quot;struct&quot; because, to my mind, that&#39;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.

[toc] | [prev] | [next] | [standalone]


#15255 — Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2012-04-08 23:35 +0200
SubjectRe: "f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<jlt0bi$l15$1@dont-email.me>
In reply to#15252
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&#39;t change the variables in the struct.
>>>
>>> I say&quot;struct&quot; because, to my mind, that&#39;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

[toc] | [prev] | [next] | [standalone]


#15270 — Re: "f() const;", defined inside a struct ( i.e. a "class" ). 

Fromnick_keighley_nospam@hotmail.com
Date2012-04-10 04:05 -0700
SubjectRe: "f() const;", defined inside a struct ( i.e. a "class" ). 
Message-ID<20623651.1424.1334055906062.JavaMail.geo-discussion-forums@vbai14>
In reply to#15249
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&#39;t change the variables in the struct.
> 
> I say &quot;struct&quot; because, to my mind, that&#39;s what it is, not a &quot;class&quot;.
> </pre>

it's a C++ class

[toc] | [prev] | [next] | [standalone]


#15256

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-04-09 08:03 +0000
Message-ID<4f8297e2$0$2798$7b1e8fa0@news.nbl.fi>
In reply to#15248
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.

[toc] | [prev] | [next] | [standalone]


#15271

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-04-10 14:12 +0000
Message-ID<slrnjo8ftr.1ls.grahn+nntp@frailea.sa.invalid>
In reply to#15256
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   .

[toc] | [prev] | [next] | [standalone]


#15272

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-04-10 14:30 +0000
Message-ID<4f844417$0$3083$7b1e8fa0@news.nbl.fi>
In reply to#15271
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'.)

[toc] | [prev] | [next] | [standalone]


#15279

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-04-10 21:57 +0000
Message-ID<slrnjo9b6k.1ls.grahn+nntp@frailea.sa.invalid>
In reply to#15272
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   .

[toc] | [prev] | [next] | [standalone]


#15257

FromBo Persson <bop@gmb.dk>
Date2012-04-09 14:22 +0200
Message-ID<9ug2k4Fav9U1@mid.individual.net>
In reply to#15248
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web