C ++使用非静态函数重载静态函数 [英] C++ Overload Static Function with Non-Static Function

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

问题描述

根据是否使用 Foo :: print()或从的实例静态调用函数,我想打印两个不同的东西。 Foo foo; foo.print();



编辑:这里是一个类定义肯定不工作, / p>

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

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

但是,有没有一个很好的方法来实现这个效果?基本上,我想做:

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

另一种方式,我知道PHP可以检查 * this 变量是否定义或不确定函数是否静态调用。

解决方案

不,它是标准直接禁止的:


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



声明不能重载




  • 仅在返回类型不同的函数声明不能​​重载。

  • 具有相同名称和相同参数类型的成员函数声明不能​​重载
    如果它们中的任何一个是成员函数声明(9.4) 。



...



>




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




>

...


此外,它也不明确,因为它可以调用实例上的静态函数:


ISO 14882:2003 C ++ Standard 9.4 / 2--静态成员

X 的静态成员 s 可能是
引用使用 qualified-id
表达式 X :: s ;使用类成员访问语法
(5.2.5)来引用静态成员是不必要的
。 A
static 成员可以使用
类成员访问语法在
中引用,其中 -expression
计算。 [示例:




 类流程{
public :
static void reschedule();
}
process& G();
void f()
{
process :: reschedule(); // OK:no object necessary
g()。reschedule(); // g()is called
}




> -end example]



...


所以,你会有歧义:

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

int main()
{
Foo f;
//调用静态或非静态成员函数?
// C ++ standard 9.4 / 2说static member
//函数可以通过这个语法调用。但
//因为还有一个名为
//print()的非静态函数,它是不明确的。
f.print();
}






无论你可以检查一个成员函数被调用的实例,还有这个关键字。 关键字指向调用函数的对象。但是,这个关键字总是指向一个对象,即它永远不会是 NULL 。因此,无法检查函数是否被静态调用,或者不是通过PHP调用。


ISO 14882:2003 C ++标准9.3.2 / 1 - 此指针



在非静态(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:

ISO 14882:2003 C++ Standard 13.1/2 – Overloadable declarations

Certain function declarations cannot be overloaded:

  • Function declarations that differ only in the return type cannot be overloaded.
  • Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration (9.4).

...

[Example:

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

—end example]

...

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

ISO 14882:2003 C++ Standard 9.4/2 – Static members

A static member s of class X may be referred to using the qualified-id expression X::s; it is not necessary to use the class member access syntax (5.2.5) to refer to a static member. A static member may be referred to using the class member access syntax, in which case the object-expression is evaluated. [Example:

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

—end example]

...

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.

ISO 14882:2003 C++ Standard 9.3.2/1 – The this pointer

In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called.

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

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