在 Main C++ 中调用成员函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/682721/
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
Calling Member Functions within Main C++
提问by user40120
#include <iostream>
using namespace std;
class MyClass
{
public:
void printInformation();
};
void MyClass::printInformation()
{
return;
}
int main()
{
MyClass::printInformation();
fgetc( stdin );
return(0);
}
How would I call the printInformation
function within main
?
The error tells me that I need to use a class object to do so.
我将如何调用里面的printInformation
函数main
?该错误告诉我我需要使用类对象来执行此操作。
回答by Daniel Earwicker
Declare an instance of MyClass, and then call the member function on that instance:
声明一个 MyClass 的实例,然后在该实例上调用成员函数:
MyClass m;
m.printInformation();
回答by Timo Geusch
If you want to make your code work as above, the function printInformation() needs to be declared and implemented as a static function.
如果你想让你的代码像上面那样工作,函数 printInformation() 需要被声明和实现为一个静态函数。
If, on the other hand, it is supposed to print information about a specific object, you need to create the object first.
另一方面,如果要打印有关特定对象的信息,则需要先创建该对象。
回答by Tamara Wijsman
From your question it is unclear if you want to be able use the class without an identity or if calling the method requires you to create an instance of the class. This depends on whether you want the printInformation member to write some general information or more specific about the object identity.
根据您的问题,不清楚您是否希望能够在没有身份的情况下使用该类,或者调用该方法是否需要您创建该类的实例。这取决于您是否希望 printInformation 成员编写一些关于对象标识的一般信息或更具体的信息。
Case 1: You want to use the class without creating an instance. The members of that class should be static, using this keyword you tell the compiler that you want to be able to call the method without having to create a new instance of the class.
情况 1:您想在不创建实例的情况下使用该类。该类的成员应该是static,使用此关键字告诉编译器您希望能够调用该方法而不必创建该类的新实例。
class MyClass
{
public:
static void printInformation();
};
Case 2: You want the class to have an instance, you first need to create an objectso that the class has an identity, once that is done you can use the object his methods.
案例2:你想让类有一个实例,你首先需要创建一个对象,以便类有一个身份,一旦完成,你就可以使用该对象的方法。
Myclass m;
m.printInformation();
// Or, in the case that you want to use pointers:
Myclass * m = new Myclass();
m->printInformation();
If you don't know when to use pointers, read Pukku's summary in this Stack Overflow question.
Please note that in the current case you would not need a pointer. :-)
如果您不知道何时使用指针,请阅读此Stack Overflow 问题中Pukku 的摘要。
请注意,在当前情况下,您不需要指针。:-)
回答by dirkgently
You need to create an object since printInformation()
is non-static. Try:
您需要创建一个对象,因为printInformation()
它是非静态的。尝试:
int main() {
MyClass o;
o.printInformation();
fgetc( stdin );
return(0);
}
回答by Jimmy J
declare it "static" like this:
像这样声明它是“静态的”:
static void MyClass::printInformation() { return; }
回答by Archit Maheshwari
you have to create a instance of the class for calling the method..
你必须创建一个类的实例来调用该方法..
回答by Sebastian Mach
On an informal note, you can also call non-static member functions on temporaries:
在非正式说明中,您还可以在临时对象上调用非静态成员函数:
MyClass().printInformation();
(on another informal note, the end of the lifetime of the temporary variable (variableis important, because you can also call non-const member functions) comes at the end of the full expression (";"))
(在另一个非正式的注释中,临时变量(变量很重要,因为您也可以调用非常量成员函数)的生命周期的结束出现在完整表达式(“;”)的末尾)