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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:39:36+00:00 2026-05-31T18:39:36+00:00

I want to overload the + operator for adding segments together to form a

  • 0

I want to overload the + operator for adding segments together to form a Path.
I’ve defined a Path where T is Segment and contains a List (where naturally T is Segment). Segment is an abstract base class for various types of Segment i.e. LineSegment

I have an overloaded method Connected which checks that the segments have a common end point. I’d like to define the overload for 2 Segments in the abstract Segment class and then for different types i.e. Segment and LineSegment within the respective derived classes.

public static Path<T> operator +(Segment s1, Segment s2)
{
    if (s1.Connected(s2))
    {
        List<Segment> slist = new List<Segment>();
        slist.Add(s1);
        slist.Add(s2);
        Path<T> p = new Path<T>(slist);
        return p;
    }
    else
    {
        return null;
    }
}

@Jon

So essentially…

I’m trying to replace the following code (path1 is a Path, Segments is a List where T is Segment).

Path<T> path1 = new Path<T>(s1);
path1.Segments.Add(s2);

with

Path<T> path1 = s1 + s2;

The problem is that with the code does not compile.

  • 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-31T18:39:38+00:00Added an answer on May 31, 2026 at 6:39 pm

    Since C# doesn’t support generic operators, I think there is no simple way to do this. But I can imagine several ways to make it work:

    1. Don’t do this. As Jon suggested, you could always return a Path<Segment>, even if you add, say, two LineSegments. I don’t like this “solution”, because it could mean you would have to use casts all over the place.

    2. Add the operator to every type that inherits from Segment. This means repeating code, but I think it’s the best option here. For example, the operator for LineSegment would look like this:

      public static Path<LineSegment> operator +(LineSegment s1, LineSegment s2)
      
    3. Don’t add two segments together, but instead add them to an empty path. If you do this, it would be probably best if you made Path immutable:

      var path = Path<LineSegment>.Empty + lineSegment1 + lineSegment2;
      
    4. Use a variant of the curiously recurring template pattern:

      class SegmentBase<T> where T : SegmentBase<T>
      {
          public static Path<T> operator +(SegmentBase<T> s1, SegmentBase<T> s2)
          {
              return new Path<T>(new List<T> { (T)s1, (T)s2 });
          }
      }
      
      class LineSegment : SegmentBase<LineSegment>
      { }
      

      If you do this, you don’t have any repetition, but it feels like a hack and it could complicate your inheritance hierarchy a lot (you can’t inherit from a class that specifies T). I don’t like this solution.

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

Sidebar

Related Questions

I have an abstract base-class to enforce some subclasses to overload the << operator.
I want to overload the operator , so that I can assign my fraction
I have a Card class and I want to overload the > operator to
I want to overload ++ operator to use pre-increment and post-increment using operator overloading
I want to overload ostream& operator<<(ostream& out, const myType& y) in order to output
I have a smart pointer class and I want to overload operator-> ; it's
I want to overload the receiver operator so I can do this: someClass >>
I want to overload the * operator in python. In C++, you can overload
I'm writing a collection class. I want to overload the square brackets operator ([])
I want to overload new operator in a template class. But something wrong happends.

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.