2

Consider the following code:

class Test
{
public:
   //1
   int kon1() const;

   //2
   const int kon2();

   //3
   static int kon3();
};

As far as I know, the difference between function 1 and 2 is that :

  1. Function 1 says that the function will not be able to change any data member's value
  2. Function 2 says that it will return a const int

(If I have wrong understanding, please correct me)

My question is : As we can see there, if we want to make a function to be const function, the const keyword is placed behind. But why in function 3, the static function, the static keyword is placed in front?

rmi
  • 532
  • 4
  • 15
William
  • 51
  • 1
  • 5
  • 2
    Because that's how the language is defined? (Specifically, this is inherited from C.) – Oliver Charlesworth Apr 03 '14 at 06:05
  • Function 1 says that none of variables or rather state of object shouldn't change but it can change state for mutable variables. – Ardel Apr 03 '14 at 06:07
  • 1
    Your code answers your own question - it can't be at the front or you couldn't tell the difference between a `const` function returning `int` and a non-`const` function returning `const int`. The choices really can seem pretty arbitrary though... e.g. `virtual` is in front while `override` is after.... – Tony Delroy Apr 03 '14 at 06:08
  • @OliCharlesworth I do know that format is defined, but I am just not so satisfied by that answer – William Apr 03 '14 at 06:13
  • @TonyD Thanks for clarifying my understanding, but I am asking the difference between function 1 2 3, as bold in my question :) – William Apr 03 '14 at 06:15
  • I suppose it is because defining a static function in global space uses this form (from C), so they keep it for classes as well. – MojaveWastelander Apr 03 '14 at 06:22
  • possible duplicate of [const static](http://stackoverflow.com/questions/177437/const-static) – jww Apr 03 '14 at 06:32

4 Answers4

3

For const member functions must have the const keyword afterwards to avoid ambiguity with the return type.

For static, virtual and other keywords having a dramatic effect on how the function works, it's desirable to list it first so it's easier to see in the class definition. For example, we can quickly scan through a list of member functions and spot all the static functions, or all the virtual ones - aiding our understanding of the overall use of the function.

Marking a member function const (or e.g. an override) is a less crucial distinction - if you have a non-const object you can invoke functions whether they're const or not, the appropriate const-ness is often obvious to the reading developer as they absorb the function return type and identifier, and in some corporate/project coding standards mutating functions are grouped above const-accessors, or const and non-const versions of the same member function are side by side to emphasise their similarities - then the differet const-ness stands out more.

All these factors combine to make the actual choices in C++ optimal for development, but you're right in observing that they're a bit inconsistent.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

The keyword static does not modify the variable's type. It modifies the memory address in which it will be located. It is used identically for function-type variables, and for data-type variables:

static int n;     // data
static int n ();  // function

The keyword const does modify the variable's type. For function-type variables, this keyword has two possible meanings:

  1. modify the function's return value as type const:

    const int n (); // function can be invoked from non-const objects only, and returns a const value

  2. modify how this function may be invoked

    int n () const; // function can be invoked const and non-const objects alike, and returns a non-const value`

Gil Elad
  • 409
  • 2
  • 6
  • In the second point " function can be invoked only from const objects". Think this can be confusing or my concept is not clear. I think it can be invoked from an object which was not declared const. Only it will act as a const object because no member variable can be modified inside that function except mutable variables. – Tahlil Apr 03 '14 at 06:44
  • It doesn't "modify the memory address". When used in a function, it modifies the lifetime of the object. When used outside of a function, it modifies the visibility of the object or function. – M.M Apr 03 '14 at 08:51
0

You are mixing two concepts i.e. Storage Class with Storage Type.

C++ have following kind of storage classes

auto, register, static, extern & mutable

And following kind of storage type (based on what u can do with on storage)

read only (can be initialized ) --> this is const

read and write --> this is non const.

So when u define a variable/function u have tell in advance what kind of storage type u want to associate. Thats why u put static as first keyword in ur code.

Hope this helps.

vikrant
  • 393
  • 4
  • 15
0
 int kon1() const;

This function is readonly function intended to work on const data only.

   const int kon2();

This function can work on modifiable object but it returns type is readonly and caller can not modify this.

CPPUser
  • 1
  • 1