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

  • Home
  • SEARCH
  • 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 6102811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:39:27+00:00 2026-05-23T13:39:27+00:00

I have a class with the operator* taking a scalar as argument, that allow

  • 0

I have a class with the operator* taking a scalar as argument, that allow me to perform the multiplication of an instance of my class with a scalar. I’d like to be able to multiply a scalar by an instance of my class (inverse order with the same result). How can I do that ?

Here an example :

class Vector3d
{
public:
    Vector3d(double x, double y, double z) {
        v[0] = x; v[1] = y; v[2] = z;
    }

    template<typename T>
    Vector3d operator*(const T s) const {
        return( Vector3d( v[0] * s, v[1] * s, v[2] * s)); 
    }

//protected: example purpose
    double v[3];
};

main()
{
    double scalar = 2.0;
    Vector3d vector(1.0,2.0,3.0);
    Vector3d v2 = vector*scalar;
    //This is the operation I want to be able to perform !
    //Vector3d v3 = scalar*vector; 
    return 0;
}

I tried to implement it like we do with ostream<< operator without success …

template<typename T>
Vector3d operator*(T& s, const Vector3d &v)
{
    return( Vector3d( v[0] * s, v[1] * s, v[2] * s));
} 
  • 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-23T13:39:27+00:00Added an answer on May 23, 2026 at 1:39 pm

    You must declare your operator* as a nonmember function (outside class) with inverse argument order and call the other one from it

    template<typename T>
    Vector3d<T> operator*(T& s, const Vector3d<T> &v)
    {
        return Vector3d(v.v[0] * s, v.v[1] * s, v.v[2] * s);
    } 
    template<typename T>
    Vector3d<T> operator*(const Vector3d<T> &v, T& s)
    {
        return s * v; //call the other overload
    } 
    

    And don’t forget specifying the template parameters:

    Vector3d<T>
            ^^^
    

    One more issue… Why take T& istead of const T& or just T? In current form you’re preventing rvalues to be passed. For example, this wouldn’t compile:

    Vector3d<int> v;
    v*3; //3 isn't an lvalue, cannot bind to a nonconst reference
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defined a user defined operator for a TCHAR*, like
I have a class that dynamically overloads basic arithmetic operators like so... import operator
I have a class that need to make some magic with every operator, like
I would like to add an operator to a class. I currently have a
In C++, can you have a templated operator on a class? Like so: class
I have class with a member function that takes a default argument. struct Class
I have a class with operator() like this: struct S { int operator()(int a,
I have an operator class that has some classes and methods in it that
I have a C++ class that overloads operator[] , the array subscript/brackets operator. This
Say I have a class like so: class Ingredient { public: friend istream& operator>>(istream&

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.