我必须创建一个对象来调用类方法吗?或者我可以只键入类名吗

Do I have to create an object to call on class methods, or can I just type in the class name?

本文关键字:我可以 或者 创建 一个对象 类方法 调用      更新时间:2023-10-16

注意-我是新手,这是我在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.

相关文章: