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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:21:16+00:00 2026-05-16T07:21:16+00:00

I have a VectorN class, and a Vector3 class inherited from VectorN (which can

  • 0

I have a VectorN class, and a Vector3 class inherited from VectorN (which can handle cross products for example). I have trouble determining the return types of the different operators. Example:

class VectorN
{
public:
   VectorN(){};
   virtual VectorN operator*(const double& d) {.....};
   std::vector<double> coords;
};

class Vector3 : public VectorN
{
public:
  Vector3(){};
  virtual Vector3 operator*(const double& d) {....};
};

This particular example produces a C2555 error:

‘Vector3::operator *’: overriding virtual function return type differs and is not covariant from ‘VectorN::operator *’, see declaration of ‘VectorN::operator *’.

The problem is that I don’t return a reference to a Vector3, and that the Vector3 class is not fully defined at the declaration of the operator*. However, I want my operator* to be virtual, and I want to return a Vector3 when I multiply a Vector3 with a constant (otherwise, if I do (Vector3*double).crossProduct(Vector3), it would return an error).

What can I do ?

Thanks!

  • 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-05-16T07:21:16+00:00Added an answer on May 16, 2026 at 7:21 am

    You need a re-design. First, prefer free-functions over member-functions. The only member functions you should have are the ones that need access to privates.

    Start with this combo:

    class VectorN
    {
    public:
       virtual VectorN& operator*=(double d)
        {
            /* ... */
    
            return *this;
        };
    };
    
    
    class Vector3 : public VectorN
    {
    public:
        virtual Vector3& operator*=(double d)
        {
            return static_cast<Vector3&>(VectorN::operator*=(d));
        };
    };
    

    Here covariance works fine because the type is a reference or pointer, and you re-use code. (static_cast is free, performance-wise, and safe since you know the derived type.)

    Then you implement your free-functions:

    // optimization: if you're going to make a copy, do it in the parameter list;
    // compilers can elide a copy when working with temporaries
    VectorN operator*(VectorN v, double d)
    {
        // reuse code
        return v *= d;
    }
    
    VectorN operator*(double d, VectorN v)
    {
        // reuse code
        return v *= d;
    }
    

    Do the same with Vector3.

    What’s been done is you get an easy way to write these operators because you get to use the core of the operator, and the return type matches, thanks to covariance.


    Do heed warnings though, you probably don’t need any of it. And extensions you want to make can be made via free-functions operating on a vector or valarray.

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

Sidebar

Related Questions

Let's say I have a base class Animal from which a class Cow inherits,
I have a class 'Vector3' which is compiled successfully. It contains both non-friend and
I have set of classes. Each class is inherited from another class. The relationship
I have a class which I intend for others to inherit from. It has
Possible Duplicate: Accessing inherited variable from templated parent class I have been implementing a
Say I have: class Vector3 { float x, y, z; ... bunch of cuntions
I have a class which has a private attribute vector rectVec; class A {
I have two classes, derived from a common class. The common class has a
i have 2 class: hObject, Drawer. Drawer inherit from hObject. with this code i
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation

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.