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

  • Home
  • SEARCH
  • 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 8349653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:04:27+00:00 2026-06-09T08:04:27+00:00

I am currently having an issue with using a function defined as a const

  • 0

I am currently having an issue with using a function defined as a const in the base class.

I am implementing a derived class using the above virtual function but with a non const return value. I would like to implement this without changing the base class as there are other outside classes that use this virtual function. The reason for not changing this virtual function is that there exists other derived classes that use the same function.

Any help greatly appreciated.

For example:
I am working on a implementing a derived class function double DerivedClass1::Function() with varying return values which is a virtual function defined in double BaseClass::Function() const. This same function is used by double DerivedClass2::function() const and double DerivedClass3::Function() const for which I have no control over. How do I do this without any or minimal changes to the base class.

  • 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-09T08:04:28+00:00Added an answer on June 9, 2026 at 8:04 am

    You have:

    struct Base {
        virtual double Function() const;
    };
    
    struct Derived1 : Base {
        virtual double Function() const;
    };
    
    struct Derived2 : Base {
        virtual double Function() const;
    };
    

    You want:

    struct Derived1 : Base {
        virtual double Function(); // overrides virtual function in base
    };
    

    So, first thing you need to add a non-const virtual function to Base, otherwise you’re not overriding anything. The question is, what should the base class function be defined as? For minimal disruption it should do the same thing that calling the function via a non-const reference currently does — call the const function:

    struct Base {
        virtual double Function() const;
        virtual double Function() { return static_cast<const Base*>(this)->Function(); }
    };
    
    struct Derived1 : Base {
        virtual double Function() const;
        virtual double Function();
    };
    
    struct Derived2 : Base {
        virtual double Function() const;
        // no need to override non-const Function
    };
    

    I think this could still potentially break existing code, for example if you took pointers to the const and non-const versions of the function, then previously they would compare equal and now they don’t:

    typedef double (Base::*constfunc)() const;
    typedef double (Base::*mutfunc)();
    ((mutfunc)(constfunc(&Base::Function)) == (mutfunc(&Base::Function))); // was true, now false
    

    For typical users of the Base class, adding the new function is probably harmless.

    However, you say: “using the above virtual function but with a non const return value”, and your example function already has a non-const return value (double) even with the const version of the function. So answers can’t address this, and it’s possible that by concealing your real use-case, you’re getting worse answers than you would with an example that better reflects your actual code.

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

Sidebar

Related Questions

I'm currently using this jQuery validate plugin and having an issue in IE where
i am having an issue, currently i am using cforms II and displaying a
I'm currently having the following issue in my project: I'm using Core Data to
I'm having an issue getting Appstats to work correctly. Using /appstats or /appstats/stats ends
I am currently having an issue with BackgroundWorker running on Windows Server 2003. I
I'm currently having an issue wrapping my brain around the notion of converting my
I'm currently having an issue storing mouse coordinates into an array in which a
Hi All I am currently having an issue calling a WCF service from a
I hope someone here can help me.. I am currently having a small issue
I'm having an issue with multiple markers on google maps - I currently have

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.