What's the difference between virtual function instantiations in C++?

后端 未结 8 1895
栀梦
栀梦 2021-01-25 07:30

What\'s the difference between the following two declarations?

virtual void calculateBase() = 0;  
virtual void calculateBase();

I read the fir

相关标签:
8条回答
  • 2021-01-25 08:01

    In Java

    virtual void calculateBase() = 0;  
    

    would be

    abstract void calculateBase();
    

    and

    virtual void calculateBase();
    

    would be

    void calculateBase();
    

    To be perfectly clear,

    void calculateBase();
    

    in C++, is "the same" as

    final void calculateBase();
    

    in Java. That is, final is "default" in C++. There is an exception to this rule however. When subclassing a class with a virtual method and reimplementing it without using the virtual keyword, the method in the subclass will not be final.

    0 讨论(0)
  • 2021-01-25 08:02

    Basically, when inheriting, you are compelled to override the first one, and are allowed to override the second one.

    Coming from Java, don't you?

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