是否可以在c++
不首先创建类实例的情况下调用类方法?
假设我们有以下代码:
// 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];
}
Run Code Online (Sandbox Code Playgroud)
这是另一个例子:
#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];
}
Run Code Online (Sandbox Code Playgroud)
我们可以看到,在上面的例子中,两个类都没有内部变量,并使用虚拟构造函数/析构函数; 他们唯一的目的是揭露一种公共方法,MyMethod(..)
.这是我的问题:假设文件中有100个这样的类(所有类具有不同的类名,但具有相同的结构 - 一个具有相同原型的公共方法 …
c++ ×1