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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:41:12+00:00 2026-05-13T15:41:12+00:00

I have a question, though it is not limited to C++. How to return

  • 0

I have a question, though it is not limited to C++. How to return totally different class from one function?

f() {

in case one: return A;
in case two: return B;
in case three: return C;


}

For example, I have two balls in the space, according to the position and the size, there are three situations for the two balls to intersect with each other, i.e, non-intersection, at point, a and circle. How can I return different class in one function?

Thanks.

  • 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-13T15:41:12+00:00Added an answer on May 13, 2026 at 3:41 pm

    If you can afford Boost then this sounds like a perfect application for Boost.Variant.

    struct NoIntersection {
        // empty
    };
    struct Point { 
        // whatever
    };
    struct Circle { 
        // whatever
    };
    
    typedef boost::variant<NoIntersection, Point, Circle> IntersectionResult;
    
    IntersectionResult intersection_test() {
    
        if(some_condition){ 
            return NoIntersection();
        }
        if(other_condition){ 
            return Point(x, y);
        }
        if(another_condition){ 
            return Circle(c, r);
        }
        throw std::runtime_error("unexpected");
    }
    

    You then process your result with a static visitor:

     struct process_result_visitor : public boost::static_visitor<> {
        
         void operator()(NoIntersection) {
            std::cout << "there was no intersection\n";
         }
         void operator()(Point const &pnt) {
            std::cout << "there was a point intersection\n";
         }
         void operator()(Circle const &circle) {
            std::cout << "there was a circle intersection\n";
         }
     };
    
     IntersectionResult result = intersection_test();
     boost::apply_visitor(process_result_visitor(), result);
    

    EDIT: The visitor class must derive from boost::static_visitor

    UPDATE: Prompted by some critical comments I’ve written a little benchmark program. Four approaches are compared:

    • boost::variant
    • union
    • class hierarchy
    • boost::any

    These are the results in my home computer, when I compile in release mode with default optimizations (VC08):

    test with boost::variant took 0.011 microseconds

    test with union took 0.012 microseconds

    test with hierarchy took 0.227 microseconds

    test with boost::any took 0.188 microseconds

    Using boost::variant is faster than a union and leads (IMO) to the most elegant code. I’d guess that the extremely poor performance of the class hierarchy approach is due to the need to use dynamic memory allocations and dynamic dispatch. boost::any is neither fast nor especially elegant so I wouldn’t consider it for this task (it has other applications though)

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

Sidebar

Related Questions

I am doing a book exercise regarding the Ackermann function. I have one question
Before answering, it is not as easy question as you might have thought about
I have gone through following question. Convert NSString to NSDictionary It is something different
I have downloaded sample code from Apple Center.I also have gone through following question:
This might be a trivial question, but I have not found anything about it,
slightly different question about variance this time. I take it from experimentation that C#
I have a more complicated issue (than question 'Java map with values limited by
Well, not much to ask apart from the question. What do you mean when
Here is a fairly easy question, though I have a hard time answering my
I have gone through this and this , but the question I am asking

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.