在没有类实例的情况下调用 C++ 类方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10442404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:03:50  来源:igfitidea点击:

Invoke a c++ class method without a class instance?

c++

提问by user58925

Is it possible to invoke a c++class method without first creating a class instance?

是否可以在c++不首先创建类实例的情况下调用类方法?

Suppose we have the following code:

假设我们有以下代码:

// just an example 
#include <iostream>
using namespace std;

class MyClass {
    public:
        MyClass();
        ~MyClass();
        int MyMethod(int *a, int *b);
};

// just a dummy method
int MyClass::MyMethod(int *a, int *b){;
    return a[0] - b[0];
}


Here's another example:

这是另一个例子:

#include <iostream>
using namespace std;

class MyClassAnother {
    public:
        MyClassAnother();
        ~MyClassAnother();
        int MyMethod(int *a, int *b);
};

// just a dummy method
int MyClassAnother::MyMethod(int *a, int *b){;
    return a[0] + b[0];
}

As we can see, in the above examples, both classes have no internal variables and use dummy constructors / destructors; Their sole purpose is to expose one public method, MyMethod(..). Here's my question: assume there are 100 such classes in the file (all with different class names, but with an identical structure -- one public method having the same prototype -- MyMethod(..).

正如我们所看到的,在上面的例子中,两个类都没有内部变量并使用虚拟构造函数/析构函数;它们的唯一目的是公开一种公共方法, MyMethod(..)。这是我的问题:假设文件中有 100 个这样的类(所有类都有不同的类名,但具有相同的结构——一个具有相同原型的公共方法—— MyMethod(..)

Is there a way to invoke the MyMethod(..)method calls of each one of the classes without first creating a class instance for each?

有没有办法调用MyMethod(..)每个类的方法调用,而无需先为每个类创建一个类实例?

回答by Greyson

Use the keyword 'static' to declare the method:

使用关键字“static”来声明方法:

static int MyMethod( int * a, int * b );

Then you can call the method without an instance like so:

然后你可以在没有实例的情况下调用该方法,如下所示:

int one = 1;
int two = 2;

MyClass::MyMethod( &two, &one );

'static' methods are functions which only use the class as a namespace, and do not require an instance.

“静态”方法是只使用类作为命名空间的函数,不需要实例。

回答by Mark Ransom

You can call a class method without an instance by declaring it static, but that doesn't appear to be what you want. You want to be able to call the same method on 100 different classes.

您可以通过声明来调用没有实例的类方法static,但这似乎不是您想要的。您希望能够在 100 个不同的类上调用相同的方法。

There are two ways for C++ to differentiate between different class methods. One is to do it at compile time, and one is to do it at run time. At compile time you need to invoke the class name itself as part of the call on a static method, or you need the object instance or pointer to be to a specific derived class. At run time you can call a method polymorphically if you have declared the method virtual. Note that you need an object of the class to use either of the run-time methods.

C++ 有两种方法来区分不同的类方法。一种是在编译时进行,一种是在运行时进行。在编译时,您需要调用类名本身作为静态方法调用的一部分,或者您需要对象实例或指向特定派生类的指针。如果您已将方法声明为虚拟方法,则在运行时您可以多态地调用该方法。请注意,您需要该类的一个对象才能使用任一运行时方法。

回答by stanigator

As Mark Random and Greyson mentioned, you can make that method static, and then invoke the right signature without instantiating an object. However, you may want to think twice about writing your function this way rather than writing a different version of that same function. Here are the considerations, especially for a large program:

正如 Mark Random 和 Greyson 所提到的,您可以将该方法设为静态,然后在不实例化对象的情况下调用正确的签名。但是,您可能要三思而后行以这种方式编写函数,而不是编写同一函数的不同版本。以下是注意事项,尤其是对于大型程序:

  • static methods are difficult to write unit tests for.
  • B/c of the nature of static methods, they reside in the memory all the time.
  • Multithreading would be difficult if your program needs to utilize it.
  • 静态方法很难为.
  • B /静态方法的性质的C,它们驻留在存储器所有的时间
  • 如果您的程序需要使用多线程,它会很困难。