Sabyc90
Sabyc90

Reputation: 133

How do read this C++ statement

I'm currently going through C++ primer Plus by Prata and came across this statement regarding *this pointer:

const Stock &topval(const Stock &s) const;

Giving that Stock is a class and topval a function ,

How do you read this statement?

I tried using the clockwise/spiral approach but I get confused by all the const. I believe that understand how to read it will allow me to better understand the concept that it is trying to prove.

Thanks!

Upvotes: 0

Views: 273

Answers (6)

g24l
g24l

Reputation: 3125

The const in the end is applied to the this pointer . The const in the argument is applied to the argument, and the const in the first part of the sentence to the return value.

simplified syntax explanation

Return value - function name - ( argument list ) - const modifier. 

The const specifier is a function modifier specifying that the pointer passed to this function is const, therefore you cannot modify member variables , except those declared mutable in the class definition.

The elaborate syntax is :

noptr-declarator ( parameter-list ) cv(optional) ref(optional) except(optional) attr(optional) -> trailing require

Therefore you declare a method That

Returns a const reference to a Stock after having received as input a const reference to a Stock object , and this pointer is a const pointer for this method being called.

The not so used but equivalent and arguable proper way to write this is

Stock const &topval( Stock const &s) const;

That now permits to spot where the cv-qualifier is actually applied (i.e. to the left-of the qualifier )

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

It'd be a lot easier if people wrote this code sensibly:

const Stock& topval(const Stock& s) const;

topval is a const function, taking a const Stock& called s, and returning a const Stock&.

Simple!

Upvotes: 1

MRodrigues
MRodrigues

Reputation: 2025

const Stock &topval( const Stock &s) const;

So you're declaring a function topval, that returns the memory address for an object of the type Stock as const. The function topval takes as an argument the memory address of Stock object. Since this is const, you will not be able to change this object inside topval.

Finally const at the end means, that this function (topval) will not be able to change any members of the class Stock..

I think it's more or less this meaning, it's been a while without c++

Upvotes: -3

DMaster
DMaster

Reputation: 641

It declares a member function (because of the last const) which:

  • Named topval

  • Do not change any other member of the class, see the last const

  • Takes only one parameter: const Stock& s

  • Returns a value of type const Stock&

Upvotes: 1

Quentin
Quentin

Reputation: 63114

             topval(              )        // topval is a member function...
                                    const; // In which *this is const...
                    const Stock &s         // Taking in a reference
                                           //     to a const Stock...
const Stock &                              // And returning a reference
                                           //     to a const Stock.

Upvotes: 13

ShadowRanger
ShadowRanger

Reputation: 155323

It's a function that both takes and returns a reference to a const Stock (not necessarily the same one, to be clear), and does not mutate this when it does so.

Upvotes: 4

Related Questions