Frage

Why doesn't the following code work?

class A
{
   static void Method() { std::cout << "method called."; }
};

class B : public A
{
   // Has a bunch of stuff but not "Method"
};

int main()
{
   B::Method();
}

I know I could make it work by adding the following to B, but it would be nice if this weren't necessary, especially if there were several classes that derived from A.

   static void Method() { A::Method(); }
War es hilfreich?

Lösung

Members of classes declared with the class key are private by default. To make them public, you have to say:

class A
{
    public:
//  ^^^^^^^
        static void Method() { cout << "method called."; }
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top