Question on Basic Function Syntax

D

Danny Gratzer

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.
 
P

Paul N

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.
 
D

danny.gratzer



"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?
 
D

danny.gratzer



&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?
 
V

Victor Bazarov

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
 
M

Marc

Victor said:
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.
 
R

Rui Maciel

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
 
A

Alf P. Steinbach

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
 
J

Juha Nieminen

Paul N said:
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.
 
B

Bo Persson

Paul N skrev 2012-04-08 21:42:
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
 
G

Guest



&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
 
J

Jorgen Grahn

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
 
J

Juha Nieminen

Jorgen Grahn said:
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'.)
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top