Added '= x' after method declaration in C ++

In C ++, when a method is declared, I noticed that sometimes a method can have an assignment attached to it.

Can anyone tell me what it is?

For instance:

virtual void MyMethod () = 0;

Which means "= 0". :)

Thanks everyone !!!

+3
source share
3 answers

This means that it is a pure virtual function, i.e. there is no real definition in this class, and it must be redefined in the subclass. This is not actually an assignment per se, zero is the only value that you can “assign”.

++; # abstract.

+6

++ , .

, . , , .

, .


, , , , .

#include <cstdio>

class A
{
public:
    virtual void foo() const = 0; //pure virtual
};

void A::foo() const { puts("A::foo"); }

class B: public A
{
public:
    virtual void foo() const { puts("B::foo"); }
};

int main()
{
    //A a;  //this would be an error - main point of having pure virtual functions
    B b;
    b.foo();
    b.A::foo();
}

, , , ( ).

+4

In C #, this is a syntax error.

If you meant C ++, see the answer "calm."

+2
source

Source: https://habr.com/ru/post/1761178/


All Articles