Equivalent of Java static methods in C++

后端 未结 1 908
别那么骄傲
别那么骄傲 2020-12-18 22:24

I\'m trying to create a method in a C++ class that can be called without creating an instance of the class (like a static method in Java), but I keep running into this error

相关标签:
1条回答
  • 2020-12-18 23:14

    In C++ it's

    Method::printStuff();
    

    and you have to declare the method as static.

    class Method{
        public:
        static void printStuff(void){
            cout << "hahaha!";
        }
    };
    

    :: is called the scope resolution operator. You can call the method with . if it's on a class instance, but the instance is not required (it being static and all...).

    0 讨论(0)
提交回复
热议问题