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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:27:14+00:00 2026-06-05T21:27:14+00:00

I have been thinking about a way solve a problem I have and I’m

  • 0

I have been thinking about a way solve a problem I have and I’m not sure whether it will work or not.

Say we have a base class called A and 3 subclass B,C and D.

        A
        ^
-----------------
|       |       |
B       C       D

Also we have three classes X, Y and Z.

On my system objects type B,C,D are passed as type A and usually I have to convert the Objects of type B,C,D to either objects X,Y or Z (That’s not a cast, I manually convert them since they are totally different).

Therefore, to convert an Object type A to a type X, Y or Z I need to check the subtype first and then initialize a X, Y or Z Object with the result of some operations about the A Object depending on the subtype.

I thought about overloading an explicit cast operation from A to X, Y or Z just doing the same process I was doing when I converted them, but then I thought… Would it be possible to use polimorfism and overload the cast from B,C and D in some way that when I add a new subtype of A I do not need to change the cast code of A?(Just add the explicit cast overload to the new subtype)

I hope I have explained myself properly sorry if there is something confusing.

NOTE: I will add a cast overload for X,Y,Z

  • 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-06-05T21:27:16+00:00Added an answer on June 5, 2026 at 9:27 pm

    You can have your cast operator for type A call a virtual (or abstract?) instance method of your A instance and override that for each subclass of A. X, Y and Z should derive from a mutual base class for this solution, though, so the cast operator can have that base class as a result type.

    public abstract class A
    {
        protected abstract W ConvertToW();
    
        public static explicit operator W(A a)
        {
            return a.ConvertToW();
        }
    }
    

    In this example, W is a base class of X, Y and Z.

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

Sidebar

Related Questions

I've been thinking about this problem for a while, and not quite sure the
I have been thinking about the optimal way to create an XML file using
I have been thinking about a neat way of load balancing and one thing
I have been thinking about the best way to store product / customer /
I have been thinking about building a client to client program. But the way
I have a problem that I have been thinking about, but I can't really
I have been thinking about this problem for long enough but cannot come up
I have been thinking about the best way to calculate the amount of on-hand
I am looking for some input on something I have been thinking about for
I'm working on a toy game engine. I have never been thinking about coordinate

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.