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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:32:13+00:00 2026-06-05T12:32:13+00:00

Given: class Base: { public: … Base operator+( const Base& other ); Base& scale(

  • 0

Given:

class Base:
{
  public:
  ...
  Base operator+( const Base& other );
  Base& scale( float num );
}

class Derived : public Base:
{
  public:
  ...
  Derived( const Base& other );
  float super_math_wizardry();
}

//A user can do the following:

Base b1,b2,b3;
Derived d1,d2,d3;

b3 = b1 + b2;
d3 = d1 + d2;

b3 = b1.scale( 2.0f );
d3 = d1.scale( 3.0f ); //scale returns Base& type that is converted to Derived

float x,y;

x = (d1+d2).super_math_wizardry(); //compiler type error since d1+d2 returns Base type
y = (d1.scale(4.0f)).super_math_wizardry(); //similar compiler error

x = Derived(d1+d2).super_math_wizardry(); //works
y = Derived(d1.scale(4.0f)).super_math_wizardry(); //works

Is there a way to make the first two statements work without re-implementing every Base method in Derived (make Derived methods that call Base methods and returns Derived types) and without requiring the user to make casts/call copy constructors?

EDIT: All Derived objects are in the set of Base objects (as is needed for the class inheritance), but not all Base objects are in the set of Derived objects. They have the same data members, but a Derived object has a constant value assigned to one of those data members (the same constant value for all Derived objects).

There are many methods that are Base or Derived specific, but most of the operators and set/get accessors have the same defined behavior on Base and Derived objects. The thing I’m trying to do is get a Derived or Derived& back when I call a Base method on a Derived object (since these operations are mathematically defined to do so), while still getting a Base or a Base& when calling a Base method on a Base object.

CONTEXT: Base is a Matrix class and Derived is a Vector (column) class. The Derived( const Base& other ) constructor was made for explicitly getting a Vector from a single column (nx1) Matrix.

So I want:

x = (d1+d2).super_math_wizardry(); //works
y = (b1+b2).super_math_wizardry(); //fails (although possibly at run-time since a nx1 Matrix is a column vector)
  • 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-05T12:32:15+00:00Added an answer on June 5, 2026 at 12:32 pm

    Given your context, I think the fundamental problem you have is informing the compiler that the set of Derived objects is closed under operator+. I know it, you know it, but there’s no special shortcut in the C++ language to express it. You do need to implement Derived Derived::operator+(const Derived&) const.

    I’d probably make the Derived(const Base &other) constructor explicit. Presumably it throws an exception if the dimensions of other are wrong, so it’s not something that users should expect to happen implicitly. They need to know it’s right, so they might as well have to say that they want it to happen.

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

Sidebar

Related Questions

assume the following class is given: class Base{ public: Base() {} Base( const Base&
In other words, given a base class shape and a derived class rectangle :
Given the base class A and the derived class B: class A { public:
Given the following snippet, class Base { public: virtual void eval() const { std::cout<<Base
Given this code: class Base{ public: Base(); virtual ~Base(); }; class Derived: protected Base{
Given the following: public abstract class Base { // other stuff public static void
Given this code: class base { public: string foo() const; // Want this to
Using VC++ 2010, given the following: class Base { }; class Derived : public
Given a base class with the following interface: public class Base { public virtual
Given the sample code: class Base { public: bool pub; protected: bool prot; };

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.