Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8278151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:54:41+00:00 2026-06-08T08:54:41+00:00

Consider this simple program: class Shape { public: virtual double getArea() = 0; };

  • 0

Consider this simple program:

class Shape
{
public:
    virtual double getArea() = 0;

};

class Rectangle : public Shape
{
    int width;
    int height;

public:
    Rectangle( int w , int h ) :width(w) , height(h) {}

    double getArea() const
    {
        return width * height;
    }
};


int main() {

    Rectangle* r = new Rectangle(4,2);
}

Trying to compile this problem gives me:

 'Rectangle' : cannot instantiate abstract class

Why is this not allowed in C++ when covariant return types are? Of course I can fix the program by making Rectangle::getArea to be a non-const function but I am curious as to why the language designers decided otherwise.

EDIT

Lots of people have mentioned in their answers how the signature is different. But so is

class Shape
{
public:
    virtual BaseArea* getArea() = 0;

};

class Rectangle : public Shape
{
public:
    virtual RectangleArea* getArea();
};

but C++ goes out of its way to allow it, when C# doesn’t.

C++ supports covariant return types because if I expect an interface to return a BaseArea* and an implemention returns a RectangleArea*, it is ok as long as RectangleArea derives from BaseArea because my contract is met.

On the same lines, isn’t an implementation that provides a non-mutating function satisfying an interface that only asks for a mutating function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:54:43+00:00Added an answer on June 8, 2026 at 8:54 am

    Most of the answer say why it’s not allowed in terms of the current rules of the language, instead of saying why the rules were written that way. I’ll try to answer why the rules couldn’t have been the way you suggest.

    Stroustrup’s Design & Evolution of C++ book describes why the overriding rules were relaxed to allow covariant returns, which weren’t always allowed in C++. So one answer to your question would be that originally overrides had to be exact matches for the signature, and an exception was made for “compatible” return types that don’t weaken the contract of a virtual function. It’s possible they just weren’t relaxed further because noone thought of it or noone suggested it. D&E does mention other possible relaxations of the overriding rules but says “We felt that the benefits from allowing such conversions through overriding would not outweigh the implementation cost and the potential for confusing users.” That’s relevant because I think your idea has plenty of potential for confusing users, and can actually cause safety problems, specifically it weakens the type system.

    Consider:

    class Square : public Rectangle
    {
    public:
      explicit Square(int side) : Rectangle(side, side) { }
    
      virtual double getArea() // N.B. non-const, overrides Shape::getArea
      {
        // class author decides this would be a sensible "sanity check"
        // (I'm not suggesting this is a good implementation)
        if (height != width)
          height = width;
        return Rectangle::getArea();
      }
    };
    
    const Square s(2);
    
    int main()
    {
    
      double (Rectangle::*area)() const = &Rectangle::getArea;
      double d = (s.*area)();
    }
    

    I believe that your idea would make this code valid, a const member function is invoked on a const object, but actually it is a virtual function so it calls Square::getArea() which is non-const and so it tries to modify a const object, which could be stored in read-only memory and so would result in a segfault.

    This is just one example of how allowing your overriding relaxation in the Shape example could result in undefined behaviour, I’m sure in more realistic code there could be bigger, maybe subtler problems.

    You could argue that the compiler should not allow a non-const function to override Rectangle::getArea and so should reject Square::getArea (“once a virtual function has gone const it can’t go back”) but that would make hierarchies very fragile. Adding or removing intermediate base classes with getArea functions with different constness would change whether Square::getArea() is an override or an overload. There is already some fragility like this with virtual functions, especially covariant returns, but according to D&E Stroustrup considered covariant returns useful because “the relaxation allows people to do something important within the type system instead of using casts.” I don’t think allowing const functions to override non-const ones fits nicely within the type-system, and doesn’t allow doing anything important, and doesn’t get rid of casts to allow a new (safe) techniques to be used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider this simple example - public class Person { private String name; private Date
Consider this simple AS3 class. package { import flash.display.Sprite; import flash.display.MovieClip; public class MySprite
Consider this simple program. The program has two files: File Vehicle.java class Vehicle {
Consider a simple program in C++: #include <iostream> class link {}; int main() {
Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
Consider this simple console application: using System; namespace Demo { class Program { static
Consider this simple markup: <body> <div style=border: 2px solid navy; position:absolute; width:100%; height:100%> </div>
Consider this code sample: public class Human { public string Value { get; set;}
Consider the following sample code: class Base { public: void f(); virtual void vf();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.