指针const vs常用指针(用于函数) [英] pointer to const vs usual pointer (for functions)

查看:80
本文介绍了指针const vs常用指针(用于函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数的const和常用指针之间有什么区别吗?当适合使用const限定词用于独立函数?

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 ++不提供警告。这是我问的原因。

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

推荐答案

您的代码格式不正确 。您可以构建一个const(或volatile)限定的函数类型。

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.

此规则已更改 C ++ 1x,以使编译器忽略 const / volatile 。 C ++编译器通常已经实现了这个规则,即使在C ++ 03模式。因此,以下两个将定义相同的函数两次,并导致编译错误。

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!

这是我的索赔证明。 C ++ 03, 8.3.5 / 1

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


A cv-qualifier- seq只能是非静态成员函数的函数类型的一部分,指向成员的指针的函数类型,或者函数typedef声明的顶层函数类型。 cv-qualifier-seq在函数声明符中的作用与在函数类型之上添加cv-qualification不同,即它不创建cv限定的函数类型。事实上,如果在确定类型的任何时候形成cv限定的函数类型,则程序是不成形的。

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.

这里是C ++ 1x的文本, 8.3.5 / 7 n2914:

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


cv-quali fi er-seq只能是非静态成员函数的函数类型的一部分,函数类型指向成员的指针,或者函数typedef声明的顶层函数类型。函数声明符中的cv-qualiter-seq的效果不同于在函数类型之上添加cv-qualial。在后一种情况下,忽略cv-qualifier。

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.

上面说的是下面的内容是有效的,并为一个可以声明一个const成员函数的函数创建函数类型。

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 { }

这篇关于指针const vs常用指针(用于函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆