我必须创建一个对象来调用类方法吗?或者我可以只键入类名吗
Do I have to create an object to call on class methods, or can I just type in the class name?
注意-我是新手,这是我在S.Overflow上的第一个问题。
我正在测试这些限制,并试图更清楚地理解所有这些编程语法。
当我尝试构建和运行这个时,它不会起作用。既然我可以输入类名,为什么要创建一个对象?
#include <iostream>
using namespace std;
class Sayings{
public:
void coolSaying(){
cout << "Preachin to the choir" << endl;
}
};
int main()
{
Sayings.coolSaying();
}
如果不想实例化对象,可以使用静态方法。但是,拥有对象实例的优点意味着您可以使用不同的数据创建同一类的多个实例。
#include <iostream>
#include <string>
using namespace std;
class Sayings{
public:
static void coolSaying(){
cout << "Preachin to the choir" << endl;
}
};
int main()
{
Sayings::coolSaying();
}
作为类实例有用性的示例:
#include <iostream>
using namespace std;
class Sayings{
public:
Sayings(const std::string & saying) : saying_(saying) {
}
void coolSaying(){
cout << saying_ << endl;
}
private:
std::string saying_;
};
int main()
{
Sayings s1("Preachin to the choir");
Sayings s2("Cool story bro");
s1.coolSaying();
s2.coolSaying();
}
是的,必须有一个类的实例才能调用其非静态成员函数。如果要在没有类实例的情况下调用成员,则必须将它们声明为静态。
class Sayings
{
public:
// static member function. Can be called without having an instance
// of Sayings
static void coolSaying()
{
cout << "Preachin to the choir" << endl;
}
// non-static member function. Requires an instance of Sayings
// to be called.
void anotherCoolSaying()
{
cout << "Preachin to the pulpit" << endl;
}
};
int main()
{
Sayings::coolSaying();
Sayings s;
s.anotherCoolSaying();
}
如果类具有"state",那么需要该类的实例的原因很好。一个类有状态,如果它有随时间变化的成员变量,那么您需要该类的一个实例来调用(非静态)成员函数。另一方面,如果该类是无状态的(或者如果该方法不影响该类对象的状态,意味着它不会更改成员变量),则可以使其成为静态成员函数。静态成员函数不需要调用类的实例。
#include <iostream>
class Widget
{
public:
Widget(): x(0) {} // constructor with initialization list
void setX(int newVal) { x = newVal; } // changes the state of an instance
void printX() { std::cout << x << std::endl; } // interacts with the state of an instance
static void printClassName() { std::cout << "Widget" << std::endl; } // doest change or interact with the state therefore can be made static
private:
int x;
};
int main(int argc, char* argv[])
{
Widget w;
w.printX();
w.setX(4);
w.printX();
Widget::printClassName();
//w::printX(); <-- this won't compile because it is not static
return 0;
}
正如您在这里看到的,输出是:04.小工具
使用点运算符.
用类的对象调用非静态类成员函数。因此,您需要首先使用类的正确构造函数创建类的对象
Sayings s; //create an object of class using default constructor
s.coolSaying(); //call methods
静态成员函数的调用使用类名,后跟作用域解析运算符::
:
Sayings::coolSaying(); //For this to work you need the following:
class Sayings{
public:
static void coolSaying(){
//^^^
cout << "Preachin to the choir" << endl;
}
};
这样想。。。如果数据不存在,那么让函数修改数据有意义吗?正如其他人所提到的,如果你想让一个函数只做与类的成员变量无关的事情,一个可行的选择就是只使用一个静态方法。
另一种选择是,如果函数根本不与类交互,那么就完全在类之外编写它。
从C++标准报价
A non-static member function may be called for an object of its class type, or for an object of a class derived (clause 10)
from its class type, using the class member access syntax (5.2.5, 13.3.1.1). A non-static member function may also be
called directly using the function call syntax.
- 函数向量_指针有不同的原型,我可以构建一个吗
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- extern可以解决这个问题吗,或者我可以通过其他方法解决这个问题吗?
- 我可以在不声明变量类型的情况下获取输入,或者在 c++ 中为同一变量声明多个类型吗?
- 我可以在生成文件中为 CC、CFLAGS 使用其他名称,或者将它们硬连接到 GNU make 中
- 我可以让 std::list 按顺序插入新元素吗?或者必须使用std::sort
- 我可以将 std::数组转换为切片吗?或者我还能用什么
- 我可以通过 G++ 将 CUDA 与C++程序一起使用吗?或者 CUDA 只能使用 GCC 编译
- 这是openMP的正确用法吗?(或者:我可以信任默认设置吗?)
- 我必须创建一个对象来调用类方法吗?或者我可以只键入类名吗
- 我是否应该复制std::函数,或者我可以总是引用它
- 我是否应该保留随机分布对象实例,或者我可以总是重新创建它
- 最少的c++学习(或者我可以跳过的)
- 我可以(a)不通过输入迭代器写入,或者(b)只通过一个迭代器读取一次吗?
- 我可以声明一个未知类型的特征矩阵吗,或者至少声明一个泛型矩阵并稍后实例化吗
- 如何(或者我可以安全地,或者我可以)移动const对象
- 对于对象的映射,我可以放置对象,或者只是成对