本文介绍了C++ 用非静态函数重载静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印两种不同的东西,这取决于函数是使用 Foo::print() 还是从 Foo foo 的实例静态调用;foo.print();

这是一个绝对不起作用的类定义,正如一些人已经回答的那样.

class Foo {字符串 bla;Foo() { bla = "非静态";}void print() { cout <<bla<

但是,有没有什么好的方法可以达到这个效果呢?基本上,我想做:

if(这是一个静态调用)做一件事别的做另一件事

换句话说,我知道 PHP 可以检查是否定义了 *this 变量来确定函数是否被静态调用.C++有同样的能力吗?

解决方案

不行,标准直接禁止:

ISO 14882:2003 C++ 标准 13.1/2 – 可重载声明

某些函数声明不能超载:

  • 不能重载仅返回类型不同的函数声明.
  • 同名同参数类型的成员函数声明不能​​重载如果其中任何一个是 static 成员函数声明 (9.4).

...

[示例:

class X {静态无效 f();无效 f();//格式错误void f() const;//格式错误void f() const volatile;//格式错误无效 g();void g() const;//OK: 没有静态 gvoid g() const volatile;//OK: 没有静态 g};

——结束示例]

...

此外,它无论如何都是不明确的,因为可以在实例上调用静态函数:

ISO 14882:2003 C++ 标准 9.4/2 – 静态成员

X 的静态成员 s 可能是使用 qualified-id 引用表达式 X::s;这不是必要的使用类成员访问语法(5.2.5) 引用一个静态成员.一个static 成员可以使用类成员访问语法,在在这种情况下 object-expression 是评估.[示例:

课程流程{上市:静态无效重新安排();}过程&G();无效 f(){流程::重新安排();//OK: 不需要对象g().重新安排();//g() 被调用}

——结束示例]

...

所以你所拥有的东西会有歧义:

class Foo{上市:字符串 bla;Foo() { bla = "非静态";}void print() { cout <<bla<

要解决有关是否可以检查成员函数正在被调用的实例的问题,有 this 关键字.this 关键字指向调用函数的对象.然而,this 关键字将始终指向一个对象,即它永远不会是 NULL.因此,无法检查函数是否在 PHP 中被静态调用.

ISO 14882:2003 C++ 标准 9.3.2/1 – this 指针

在非静态(9.3)的主体中成员函数,关键字this是一个非左值表达式,其值为对象的地址函数被调用.

I would like to print two different things depending on whether a function is called statically with Foo::print() or from an instance of Foo foo; foo.print();

EDIT: Here is a class definition that definitely does not work, as answered by a few people already.

class Foo {
    string bla;
    Foo() { bla = "nonstatic"; }

    void print() { cout << bla << endl; }
    static void print() { cout << "static" << endl; }
};

However, is there a good way of achieving this effect? Basically, I would like to do:

if(this is a static call)
    do one thing
else
    do another thing

Phrased in another way, I know PHP can check if the *this variable is defined or not to determine whether the function is called statically. Does C++ have the same capability?

解决方案

No, it is directly prohibited by the standard:

class X {
    static void f();
    void f();                // ill-formed
    void f() const;          // ill-formed
    void f() const volatile; // ill-formed
    void g();
    void g() const;          // OK: no static g
    void g() const volatile; // OK: no static g
};

Besides, it would be ambiguous anyway since it's possible to call static functions on instances:

class process {
public:
        static void reschedule();
}
process& g();
void f()
{
        process::reschedule(); // OK: no object necessary
        g().reschedule();      // g() is called
}

So there would be ambiguity with what you have:

class Foo
{
public:
    string bla;
    Foo() { bla = "nonstatic"; }
    void print() { cout << bla << endl; }
    static void print() { cout << "static" << endl; }
};

int main()
{
    Foo f;
    // Call the static or non-static member function?
    // C++ standard 9.4/2 says that static member
    // functions are callable via this syntax. But
    // since there's also a non-static function named
    // "print()", it is ambiguous.
    f.print();
}


To address your question about whether you can check what instance a member function is being called on, there is the this keyword. The this keyword points to the object for which function was invoked. However, the this keyword will always point to an object i.e. it will never be NULL. Therefore it's not possible to check if a function is being called statically or not à la PHP.

这篇关于C++ 用非静态函数重载静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 20:57
查看更多