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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:08:22+00:00 2026-06-12T19:08:22+00:00

I have the following simple class class base { public: int x; base &set(int

  • 0

I have the following simple class

class base
{
public:
  int x;
  base &set(int y)
    {
      x = y;
      return *this;
    }
};

and want to create a new one with added functionality, say to print the value x. So I do:

class derived : public base
{
public:
  void print()
    {
      cout << x << endl;
    }
};

Now in the main program I want to do something like

D.set(2).print();

for which however the compiler complains that class base has no member named ‘print’.

If I try to use covariant return types and write the two classes as

class base
{
public:
  int x;
  virtual base &set(int y)
    {
      x = y;
      return *this;
    }
};

class derived : public base
{
public:
  derived &set(int y)
    {
      x = y;
      return *this;
    }
  void print()
    {
      cout << x << endl;
    }
};

then the statement works just fine, but I was forced to rewrite the exact same function body for ‘set’ in both classes, even though the only thing that changes is the return type.

If later on I need to change the functionality of base::set then I will have to go through all the derived classes to change the ‘set’ function… are there any ways to avoid that? Thanks in advance!

  • 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-12T19:08:23+00:00Added an answer on June 12, 2026 at 7:08 pm

    C++ work as you say, and you say in your base class set return base&, so that is what C++ do. But in order to solve this you have many ways.

    First you are not forced to make a function virtual to override it in a derived class (note that virtual call are slightly slower than normal calls).

    Second you can refer to base class implementation as base::set so the code will be as follow:

    class base {
        ...
        base& set( int x ) {...}
    };
    class derived : public base {
        derived& set( int x ) {
            return static_cast<derived&>( base::set(x) );
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following (trivially simple) base class: public class Simple { public
I have the following simple textbook classes defined: public class Product { public int
I have the following simple code abstract class A { public abstract void Test(Int32
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following class (from a simple Spring tutorial) public class CarValidator implements
Basically I have a simple base class: public abstract class BasePoco { public virtual
I have the following code that doesn't compile. class Base { public: virtual ~Base()
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have the following simple class definition: //mycommon.h @interface CurrentPath : NSObject @property (nonatomic,
Say I have the following simple java bean: class MyBean { private Date startDate;

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.