William
William

Reputation: 51

c++ - difference between const and static keyword in function header

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?

Upvotes: 2

Views: 2533

Answers (4)

CPPUser
CPPUser

Reputation: 1

 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.

Upvotes: 0

Tony Delroy
Tony Delroy

Reputation: 106236

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.

Upvotes: 3

Gil Elad
Gil Elad

Reputation: 409

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`

Upvotes: 0

vikrant
vikrant

Reputation: 433

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.

Upvotes: 0

Related Questions