3

I am creating a class diagram for a class that has many definitions similar to this one inside of its header file.

  1       2                                        3             4       5
const std::pair<Controller<_Tp,_Val>*, _Val>& getHighestBidder(_Tp obj) const;

I know what several of them do,

2) says that this method will return a std::pair<Controller<_Tp, _Val>*, _Val>
3) gives the name of the function
4) defines the type of object this function accepts as a parameter

But, what do 1 and 5 mean?

Any help/pointers would be great. Thanks

Maxpm
  • 24,113
  • 33
  • 111
  • 170
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170

5 Answers5

9

Firstly, note that it's not returning a std::pair<Controller<_Tp, _Val>*, _Val>, it's returning a std::pair<Controller<_Tp, _Val>*, _Val> &, i.e. a reference to such an object that already exists.

(1) denotes that it's a const reference to an object; you cannot modify the object through this reference.

(5) denotes that this is a const member function, i.e. it doesn't modify the object it was called on.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • I think also important to highlight a couple things we might take for granted. 1) this is (must be) a member function. 2) The `this` pointer in this member pointer is `const`, and therefore 3) This member function may not modify any member values, except those qualified as `mutable`. – John Dibling Sep 14 '11 at 19:55
3

It means exactly the same, but I prefer to write it as:

  1                                      2       3              4          5
std::pair<Controller<_Tp,_Val>*, _Val> const& getHighestBidder(_Tp obj) const;


A member function
   5: const member function
   4: Taking a parameter of type _Tp by value
   3: With a function name getHighestBidder
Returning
   2: A const reference
   1: To a std::pair<>
      1a: first  member has type Controller<_Tp,_Val>*
      1b: second member has type _Val


5: Means calling this method will not change the state of the object
2: Means the returned object can not be altered via the reference returned
   This is probably because it is an alias of an internal member and if you
   could alter the object you would invalidate the const mention at (5).
John
  • 15,990
  • 10
  • 70
  • 110
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • P.S. he prefers to write it that way so he can read it right-to-left kind of like a compiler. – AJG85 Sep 14 '11 at 20:15
  • @AJG85: What do you mean `kind of like a compiler`. You think the compiler cares which end it is on? – Martin York Sep 14 '11 at 20:28
  • @Tux-D: No compilers are smart these days it will apply keywords and change the resulting instructions as it encounters them. `const` as a keyword is applied to the thing immediately to the left unless there is nothing to the left in which case it applies to the right. I just meant what the compiler will do with it makes more sense reading right-to-left if you always put `const` to the right. – AJG85 Sep 14 '11 at 20:33
  • @ AJG85: No and no. In my opinion it makes more sense reading right to left (hence that is the way I prefer to write it) though I understand and respect that other disagree. To the compiler it has never made any difference (nor will it) it just finds the appropriate type object in the symbol table. – Martin York Sep 14 '11 at 20:54
1

(1) means that the returned object is a const reference, you can only call const methods of the object to which it refers.

(5) means that the method is const: you can call it using a const reference or pointer (and it can't modify and members).

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Are you sure about number 5? I've always understood it to mean that it couldn't modify member variables. – N_A Sep 14 '11 at 19:53
  • @mydogisbox: yes, that is what it really means. But because it is `const`, it can also be called through `const` references/pointers as well. – Remy Lebeau Sep 14 '11 at 20:02
  • @bmargulies Perhaps you should include that in your answer? – N_A Sep 14 '11 at 20:03
1
  1. means the return type is constant (i.e. can't be modified)
  2. means that the method cannot modify any member variables of the class it belongs to

For a more detail explanation see:

Use of ‘const’ in Functions Return Values

and

Messier Still - in the Object Oriented Programming

in the following link:

http://duramecho.com/ComputerInformation/WhyHowCppConst.html

N_A
  • 19,799
  • 4
  • 52
  • 98
1

When you add "const" to a variable/class declaration, it makes it unchangable by the compiler, sort of like the "define" feature in objective-c. It is extremely useful because it makes errors that would normally just crash the compiler into errors that the compiler can pick up (because you are trying to change the value of a constant).

const int randomInteger = 5;
randomInteger = 3;

You see, this would return an error, which makes debugging easier.

A good web resource for "const" is here :

http://duramecho.com/ComputerInformation/WhyHowCppConst.html

Monkeyanator
  • 1,346
  • 2
  • 14
  • 29