Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer to const vs usual pointer (for functions)

Is there any difference between pointer to const and usual pointer for functions? When it is suitable to use const qualifier for stand alone functions?

I wrote short sample to illustrate my question:

#include <iostream>
using namespace std;

int sum( int x, int y ) { return x + y; }
typedef int sum_func( int, int );

int main()
{
    const sum_func* sum_func_cptr = &sum; // const function
    sum_func* sum_func_ptr = &sum;        // non-const function ?

    // What is the difference between sum_func_cptr and sum_func_ptr

    int x = sum_func_cptr( 2, 2 );
    cout << x << endl;

    int y = sum_func_ptr( 2, 2 );
    cout << y << endl;

    sum_func_cptr = 0;
    sum_func_ptr = 0;

    return 0;
}

g++ gives no warnings. That's why I ask.

like image 374
Kirill V. Lyadvinsky Avatar asked Jul 13 '09 05:07

Kirill V. Lyadvinsky


People also ask

What is the difference between constant pointer and pointer pointing to constant?

In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable. but we can change the value at the pointer. Pointer to Constant: A pointer to a constant is a pointer that cannot change the value of the address its holding through the pointer.

Can you make pointers to const?

A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value. In the above example, ptr points to a const int . Because the data type being pointed to is const, the value being pointed to can't be changed. We can also make a pointer itself constant.

What does pointer to const mean?

A pointer to constant is a pointer through which the value of the variable that the pointer points cannot be changed. The address of these pointers can be changed, but the value of the variable that the pointer points cannot be changed.

What happens if you try to change a const?

Changing Value of a const variable through pointerThe compiler will give warning while typecasting and will discard the const qualifier. Compiler optimization is different for variables and pointers.


1 Answers

Your code is ill-formed with regard to C++03. You can not ever construct a const (or volatile) qualified function type. Whenever you do, your program becomes ill-formed.

This rule has been changed for C++1x, to make the compiler ignore the const / volatile. C++ compilers will usually already implement this rule even in C++03 mode. Thus, the following two will define the same function twice, and results in a compilation error.

typedef void Ft();


void f(Ft const*) { }
void f(Ft *) { } // another definition!

Here is the proof of my claim. C++03, 8.3.5/1

A cv-qualifier-seq shall only be part of the function type for a nonstatic member function, the function type to which a pointer to member refers, or the top-level function type of a function typedef declaration. The effect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of the function type, i.e., it does not create a cv-qualified function type. In fact, if at any time in the determination of a type a cv-qualified function type is formed, the program is ill-formed.

Here is that text for C++1x, 8.3.5/7 n2914:

A cv-qualifier-seq shall only be part of the function type for a non-static member function, the function type to which a pointer to member refers, or the top-level function type of a function typedef declaration. The effect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of the function type. In the latter case, the cv-qualifiers are ignored.

The above says that the below is valid, though, and creates the function type for a function that can declare a const member function.

typedef void Ft() const;
struct X { Ft cMemFn; };
void X::cMemFn() const { }
like image 167
Johannes Schaub - litb Avatar answered Nov 19 '22 10:11

Johannes Schaub - litb