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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:03:11+00:00 2026-05-25T18:03:11+00:00

I was trying to make virtual operator on C++ class Data { virtual Matrix

  • 0

I was trying to make virtual operator on C++

class Data
{
    virtual Matrix operator* (Matrix &_matrix);
    virtual Scalar operator* (Scalar &_scalar);
};

class Matrix : public Data
{
private:
    vector<vector<double>> data;
public:
    // ...
    Matrix operator* (Matrix &_matrix);
};

class Scalar : public Data
{
private:
    double data;
public:
    // ...
    Scalar operator* (Scalar &_scalar);
};

And the problem was, when I created Data* Array like below

Data* arr[10];
arr[0] = new Matrix(3,3);
arr[1] = new Matrix(3,3);

arr[0]->operator*(arr[1]);

I could not do multiplication between these two matrices, since I cannot pass Data as an argument.
But the problem is, I cannot make the function’s argument to take Data* type because, it will not be able to access private members of Matrix or Scalar objects.

How to deal with this kind of weird situation?

  • 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-25T18:03:12+00:00Added an answer on May 25, 2026 at 6:03 pm

    Class double dispatch problem – See Meyer’s (forget which one).
    You need the operator to be virtual on both the lhs and rhs, so you need two virtual calls:

    class Matrix;
    class Scalar;
    
    class Data
    {
    public:
        virtual Data* operator* (Data& data) = 0;
        virtual Data* operator* (Matrix &matrix) = 0;
        virtual Data* operator* (Scalar &scalar) = 0;
    };
    
    class Matrix : public Data
    {
    private:
        std::vector<std::vector<double>> data;
    
        // ...
    public:
        Matrix* operator* (Matrix &_matrix)
        {
            // implement
        }
        Matrix* operator* (Scalar& scalar)
        {
            // implement
        }
        Data* operator* (Data &data)
        {
            // Magic here - *this is now Matrix, not Data
            return data * (*this);
        }
    };
    
    class Scalar : public Data
    {
    private:
        double data;
    public:
        // ...
        Data* operator* (Data& data)
        {
            // Magic here - *this is now Scalar, not Data
            return data * (*this);
        }
        Scalar* operator* (Scalar &scalar)
        {
            // implement
        }
    
        Matrix* operator* (Matrix &matrix)
        {
            // Note how we need to allow for parameter reveral during the double dispatch
            Matrix& lhs = matrix;
            Matrix& rhs = *this;
            // Compute matrix product lhs * rhs
        }
    
    };
    

    I’ve glossed over the issue of the return type and memory management. As usual with operators, you might be better defining *= as the primitive. This can then return a reference to *this. The * operator can then defined in terms of *=. Once again, this is in “Effective C++”.

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

Sidebar

Related Questions

Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a custom :confirm message for a rails form that returns data
What I'm trying to do is, make the class message serialize and deserialize it's
I'm trying to make each instance of a class (named Caller here) have an
I have an abstract class in a library. I'm trying to make it as
I'm trying to make the root directory for a virtual host so that it
I am trying to make a Visual C++ 2008 program that plots some data
I am trying to make a class that contains two other objects, and those
I am trying to make a configuration manager class, that can store arbitrary objects
In my application I am trying to make a virtual attribute that's a date_select

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.