![](/img/trans.png)
[英]statement of creating an object without using new operator in c++ is treated as a variable declaration statement or a function call statement?
[英]How to call a class function in c++ without object declaration?
我在 class 中创建了一个 function。 我如何在 int main() 中调用 function 而不为 class 声明任何 object?
你可以做的是将你的 function 写在 class 中作为 static。 这使您可以在不声明 Object 的情况下调用 function
一种可能的方法如下所示:
#include <iostream>
class NAME
{
public:
//define a member function inside this class
void print()
{
std::cout<<"print callled"<<std::endl;
}
};
int main()
{
//create NAME object. This defines a variable named n of type NAME
NAME n;
//use object n to call print member function
n.print();
//you can also call print() member function on a temporary object
NAME().print();
return 0;
}
此外,您的意思也不是很清楚
没有声明 class 名称?
但是我举了一个例子,认为/考虑到您无法正确描述您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.