C++ 从另一个方法调用非静态成员方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9713430/
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
Call a Non Static Member Method from Another Method
提问by aclark
Is there a way to call a non static class member method from another method that is contained within the main class in c++? If so, what would the code look like?
有没有办法从包含在 C++ 主类中的另一个方法调用非静态类成员方法?如果是这样,代码会是什么样子?
Problem is, I can't declare this specfic method as static, because it uses other methods within the same class that then don't work if I make the one static.
问题是,我不能将此特定方法声明为静态,因为它使用同一类中的其他方法,如果我将其设为静态,则这些方法将不起作用。
I'm trying to use:
我正在尝试使用:
MyClass::myClassMethod();
from a method within the main class, but it gives me the error: a non static member reference must be relative to a specific object.
来自主类中的方法,但它给了我错误:非静态成员引用必须相对于特定对象。
To clarify, myClassMethod() uses other methods within MyClass like:
澄清一下,myClassMethod() 使用 MyClass 中的其他方法,例如:
void myClassMethod() {
...
anotherClassMethod();
}
so if I were to make myClassMethod static it would interfere with calling anotherClassMethod().
因此,如果我将 myClassMethod 设为静态,它会干扰调用 anotherClassMethod()。
回答by Alok Save
What is the deal with calling non-static member function from a static
member function?
从static
成员函数调用非静态成员函数是怎么回事?
Every non static member function is passed an this
pointer implicitly in addition to the parameters you pass, the pointer passed is then dereferenced to refer class object members However static
functions are not passed with the implicit this
pointer and hence one cannot call any non static member function inside a static member function because there is no this
to do so.
this
除了您传递的参数之外,每个非静态成员函数都会隐式传递一个指针,然后将传递的指针取消引用以引用类对象成员但是static
函数没有通过隐式this
指针传递,因此不能在内部调用任何非静态成员函数静态成员函数因为没有this
这样做。
What is the solution, If you want to do it anyways?
解决方案是什么,如果您仍然想这样做?
You will need some mechanism to get the pointer to the object inside the static method and then you can call the member function using that pointer.
How to do that?
You will have to store the pointer to class object globally, or pass it as an instance in one of the function arguments to the static method.
您将需要某种机制来获取指向静态方法内的对象的指针,然后您可以使用该指针调用成员函数。
怎么做?
您必须全局存储指向类对象的指针,或将其作为实例传递给静态方法的函数参数之一。
However, both of above are workarounds, the important thing to note here is If you feel the need of calling a non static member function through a static member function then there is something wrong in your design.
但是,以上都是变通方法,这里要注意的重要一点是,如果您觉得需要通过静态成员函数调用非静态成员函数,那么您的设计就有问题。
On Second thoughts maybe I mis-read your Question before, Probably, Your question is:
再想一想,也许我之前误读了您的问题,可能,您的问题是:
How to call a non-static member function of a class from main
?
如何调用类的非静态成员函数main
?
You need a instance of the class to call non-static member functions.
So simply,
您需要一个类的实例来调用非静态成员函数。
那么简单,
MyClass obj;
obj.myClassMethod();
And calling any other member function from within myClassMethod()
would simply be:
从内部调用任何其他成员函数myClassMethod()
只是:
void myClassMethod()
{
//...
anyOtherMyClassNonStaticMemberFunction();
//...
}
回答by Wyzard
A static method is one that doesn't run on any particular object. It's a lot like a standalone function outside of a class, except that it's allowed to access private members in its class.
静态方法是一种不在任何特定对象上运行的方法。它很像一个类之外的独立函数,只是它允许访问类中的私有成员。
If you anotherClassMethod()
is non-static, that means it has to be called on a specific object, an instance of the class. Because it's called on an object, it can access data stored in that object (non-static member variables). If myClassMethod()
is static and you implement it as
如果您anotherClassMethod()
是非静态的,则意味着必须在特定对象(类的实例)上调用它。因为它是在对象上调用的,所以它可以访问存储在该对象中的数据(非静态成员变量)。如果myClassMethod()
是静态的并且您将其实现为
void MyClass::myClassMethod() {
anotherClassMethod();
}
That won't work because anotherClassMethod()
expects to be called on a specific object, but myClassMethod()
doesn't have one. But if you know what object you want to call it on, you can do it as an ordinary method call on an object:
这是行不通的,因为anotherClassMethod()
期望在特定对象上调用,但myClassMethod()
没有。但是,如果您知道要在哪个对象上调用它,则可以将其作为对对象的普通方法调用来执行:
void MyClass::myClassMethod(MyClass &object) {
object.anotherClassMethod();
}
The object
doesn't have to be passed in as an argument; it could be a static member variable in the class, for example:
在object
不具有作为参数传递; 它可以是类中的静态成员变量,例如:
class MyClass {
private:
static MyClass theInstance;
// ...
};
void MyClass::myClassMethod() {
theInstance.anotherClassMethod();
}
Ultimately, the question you need to ask yourself is: why is myClassMethod()
static, and why is anotherClassMethod()
non-static? Take a step back, think about what myClassMethod()
is supposed to do. Does it make sense to call it when you don't have an instance to work with? If so, why does it need to call a method that expects to work with an instance?
最终,您需要问自己的问题是:为什么是myClassMethod()
静态的,为什么anotherClassMethod()
是非静态的?退后一步,想想myClassMethod()
应该怎么做。当您没有可使用的实例时,调用它是否有意义?如果是这样,为什么它需要调用一个期望与实例一起工作的方法?
回答by Lou
The only way to call a non static method of a class is through an instance of that class. So you would need something like this...
调用类的非静态方法的唯一方法是通过该类的实例。所以你需要这样的东西......
MyClass myClass;
myClass.myClassMethod();
回答by wangdxh
I think that maybe you could use the singleton pattern, keep a instance of the class in global. like a utility class.
我认为也许您可以使用单例模式,将类的实例保留在全局中。就像一个实用程序类。