本文介绍了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 变量是否定义或不确定函数是否静态调用。

解决方案

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



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



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



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



所以,你会有歧义:

  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调用。


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
查看更多