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 7762543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:25:58+00:00 2026-06-01T14:25:58+00:00

Possible Duplicate: c++ virtual function return type I have a simple but confusing question

  • 0

Possible Duplicate:
c++ virtual function return type

I have a simple but confusing question here. Is it legal to have a different return value type for overridden methods than the abstact ones defined in the base class?? I did that and the compiler didn’t complain… could someone please explain?

class MyBaseClass
{
    int value;

public:
    virtual int getValue() = 0;
};

class MyClass : public MyBaseClass
{
    double value;

public:
    virtual double getValue(); // here!!! return is double, not int
};

double MyClass::getValue()
{
   return this->value;
}

The compiler totally accepted something similar (MSVC und MinGW)… could anyone please exaplain to what extent this is legal?

  • 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-01T14:25:59+00:00Added an answer on June 1, 2026 at 2:25 pm

    The return type is allowed to differ but only in a bery restrictive way and you code is illegal. The only way the return type of an override is allowed to differ is that can be covariant if yhe return type of the base is a pointer or a reference. Put differently: if the base returns a pointer or a reference to a base class the overrude is allowed to return a pointer or a reference, respectively, to a class derived of the base.

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

Sidebar

Related Questions

Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question
Possible Duplicate: Can someone explain C++ Virtual Methods? I have a question regarding to
Possible Duplicate: C++ static virtual members? Can we have a static virtual functions? If
Possible Duplicate: How to get the address of an overloaded member function? I have
Possible Duplicate: Why pure virtual function is initialized by 0? In C++, what does
Possible Duplicate: Pure virtual functions may not have an inline definition. Why? I've come
Possible Duplicate: Pure virtual destructor in C++ I have two classes: the abstract Game
Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
Possible Duplicate: Why are private virtual methods illegal in C#? I have the following
Possible Duplicate: How to force child same virtual function call its parent virtual function

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.