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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:35:07+00:00 2026-06-12T23:35:07+00:00

I have a fairly complicated construct. I’m also fairly certain I’ve been staring at

  • 0

I have a fairly complicated construct. I’m also fairly certain I’ve been staring at it for way too long, and all those trees now obscure my view of the forest. So I’ll give you the full complication of my construct, even though I suspect only a small portion of it is actually relevant.

Now, my construct in words:

  • A templated baseclass, derived from its own baseclass, implements several operator overloads.

  • Classes subclassed from the templated baseclass (using their own name as template parameter) will have access to all the operators. This construct ensures that operators can only operate on equal types, as per this question.

  • A single specific subclass implements a few specialized versions of the operators. Call this class Derived. This subclass also uses named constructors, so its main constructor is private.

  • Another baseclass implements cast operators to double and to Derived, to enable its subclasses to be (implicitly) cast to Derived type. Call one of these base classes OtherDerived.

  • Instances of Derived can then be constructed either by a call to one of the the named constructors in Derived, or by a pass through OtherDerived as in Derived D = OtherDerived(5.0);

My problems:

  1. Defining Derived D = OtherDerived(5.0) * 2.0; seems to cast OtherDerived(5.0) to double instead of Derived, so that the multiplication by 2.0 is simply a product of doubles, and NOT the output of operator* in Derived‘s base class.

  2. The definition of operator*= in the templated base class cannot return a reference to *this, since the type of *this is BaseClass<T> while the desired type of the reference is just T&.

How to resolve these issues elegantly?

I consider the second problem minor, since I can easily work around it by not returning a reference at all. Still, it would be nice to have one. Most important to me though is: how do I enable people to write Derived D = OtherDerived(...) * 2.0 or some similar, simple form?

Demanding people to write

  • Derived D = (Derived)OtherDerived(...) * 2.0;, or
  • Derived D = OtherDerived(...) * Derived::someName(2.0);, or
  • Derived D = OtherDerived(...).operator*(2.0);

etc. seems rather strange and unnecessary…

NB: preferably, the operator double() remains available 🙂

Here is an MWE:

#include <iostream>
#include <cmath>

class SuperBase
{
public:
    virtual ~SuperBase(){}
    SuperBase() : value(0.0) {}

protected:
    double value;
    SuperBase(double value) : value(value) {}
};

template <class T>
class Base : public SuperBase
{
public:
    virtual ~Base(){}
    Base() : SuperBase() {}

    T& operator*=(double F)       { value *= F; return *this; }
    T  operator* (double F) const { return T(value*F); }

    double operator*(const T& U) const {
        return value*U.value;
    }

protected:
    double value;
    Base(double value) : SuperBase(value) {}
};

class Derived final : public Base<Derived>
{
public:

    ~Derived(){}
    Derived() : Base<Derived>(){}

    static Derived someName(double value) {
        return Derived(value);
    }

    Derived operator*(const Derived& D) const {
        return Derived(value/D.value);
    }

private:
    Derived(double value) : Base<Derived>(value) {}
};




class OtherBase
{
public:
    virtual ~OtherBase(){}

    operator double () { return value; }
    operator Derived() { return Derived::someName(value); }

protected:
    double value;
};

class OtherDerived final : public OtherBase
{
public:
    ~OtherDerived(){}

    OtherDerived(double value){
        this->value = std::sqrt(value);
    }
};



int main(int argc, char *argv[])
{
    Derived a = OtherDerived(1.05);     // Compiles fine

    Derived b = OtherDerived(1.05);     // Compiles fine
    b *= 2.0;                           // Error: cannot cast Base<Derived>
                                        // to Derived

    Derived c = OtherDerived(1.05)*2.0; // Error: casts the
                                        // OtherDerived to double,
                                        // so Derived(double) gets
                                        // called, which is private

    return 0;
}
  • 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-12T23:35:09+00:00Added an answer on June 12, 2026 at 11:35 pm

    In case #2 of your main code, your problem seems to stem from the fact you would like your Base<Derived> class to create a Derived object inside of Base<Derived>::operator*= when the constructor for Derived is private … you’ll have to make the Base<Derived> class a friend of Derived in order for the constructor to be called.

    For case #3 of your main code, I would define a operator* for your OtherBase or OtherDerived class to prevent the implicit casting to double.

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

Sidebar

Related Questions

I have a fairly complicated installer that I'm writing in Wix that has a
I am working on a fairly complicated project. We have a c# forms app
I have a fairly complicated lookup i'm trying to do in Rails and I'm
In an application I'm writing I have a fairly complicated Database model. I'd like
this is my first posted question. I have a fairly complicated OQL query which
Just out of curiosity, for applications that have a fairly complicated module tree, would
Judging from the title, I kinda did my program in a fairly complicated way.
I have a fairly complicated html table containing lots of PHP, CSS etc., and
I have a project that has a fairly complicated, nested (references calling the referenced
I have a build process that creates an ear in a fairly complicated manner

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.