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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:09:58+00:00 2026-05-23T23:09:58+00:00

Suppose I have a class: public class Vector { public float X { get;

  • 0

Suppose I have a class:

 public class Vector
 {
   public float X { get; set; }

   public Vector(float xvalue)
   {
       X = xvalue;
   }

   public static Vector operator +(Vector v1, Vector v2) 
   {
       return new Vector(v1.X + v2.X);
   }
 }

Have a derived class:

 public class MyVector : Vector
 {
     public static MyVector operator +(MyVector v1, MyVector v2)
     {
         return new MyVector(v1.X + v2.X);
     }

    public MyVector(float xvalue):base(xvalue)
    {

    }
 }

No if I execute folowing code:

 Vector v1 = new MyVector(10);    //type MyVector

 Vector v2 = new MyVector(20);    //type MyVector

 Vector v3 = v1 + v2;             //executed operator is of Vector class

Here is executed Vector’s + operator, but the type of v1 and v2 is MyVector, so v3 is Vector type at this point.

Why this happens?

  • 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-23T23:09:59+00:00Added an answer on May 23, 2026 at 11:09 pm

    Because the type of the variables v1 and v2 are Vector, not MyVector. Operator overloads are static methods that are resolved by the compiler at compile-time, not at runtime; they cannot be overridden.

    v1 and v2 will have to be typed MyVector for the compiler to select the overload defined in the MyVector class.

    Optionally, define a method public virtual Vector Add(Vector other) on the Vector class and have MyVector override it. Then call this method from the operator+ method of Vector and this will work as you expected. (MyVector would then not need to define operator+ itself.)

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

Sidebar

Related Questions

Suppose I have a class public class MyClass { private Set<String> set = new
Suppose I have a class with overloaded operator new . class Class { public:
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Say Suppose you have a class public class Person { public int PesronId{get;set;} public
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have this class: public class DispatcherService<T> { private static Action<T> Dispatcher; public
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
Suppose I have the following class: public class TestBase { public bool runMethod1 {
For example, suppose I have a class: class Foo { public: std::string& Name() {
Suppose you have a collection of Foo classes: class Foo { public string Bar;

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.