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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:39:46+00:00 2026-06-12T09:39:46+00:00

Suppose you have something like the following: class Shape // base class { private:

  • 0

Suppose you have something like the following:

class Shape  // base class
{
private:
    bool degenerate, ill_defined;
    ...
public:
    bool isVoid  () { return false; }
    bool isCircle() { return false; }
    bool isPoint () { return false; }
    bool isPlane () { return false; }
    bool isSphere() { return false; }
    ...
};

class Void : public Shape {
    ...
}

class Plane : public Shape
{
public:
    bool isPlane() { return !degenerate && !ill_defined; }
    bool isVoid () { return ill_defined; }
    ...
    operator Void () throw() { 
        if (isVoid()) return Void(); 
        else throw ...; //some error
    }
    ...
}

class Point : public Shape {
private:
    double radius;
    ...
public:
    bool isPoint() { return !ill_defined; }
    bool isVoid () { return ill_defined; }
    ...        
    operator Void () throw() { ... }
    ...
}

class Circle : public Shape // similar to the rest

class Sphere : public Shape // similar to the rest

The intersection between a Plane and a Sphere can be either

  • a Circle (if the plane “cuts through” the sphere)
  • a Point (if the plane “just touches” the sphere)
  • a Void (if the sphere lies entirely above or below the plane)

I was wondering how to best define and use an intersection between a Plane and a Sphere, since the return type of the hypothetical

intersect(const Sphere& S, const Plane& P)

method/free function is unknown at compile time.

I never encountered this situation before, so I looked up some possible ways to do it. I came across this question which recommends boost::variant. In my situation, that would look like

boost::variant<Void, Point, Circle> intersection = 
    intersect(const Sphere& S, const Plane& P);

But this has three drawbacks:

  1. It’s fugly.
  2. something like intersection.radius cannot be used as-is, since Point and Void do not have a radius. You’d have to do something like

    if (intersection.isPoint()){
        ...
    }
    else if (intersection.isCircle())
    {
        // possibly cast to Point if degenerate, otherwise:
        double R = intersection.radius;
        ...
    }
    // etc.
    
  3. The user of a library implementing all these shapes would always have to know what types could be returned by intersecting two shapes. That is, the user would always have to declare something of type boost::variant<scope::Void, scope::Point, scope::Circle> which is complicated and just plain ugly. Fortunately, c++11 has the auto keyword for that. Alternatively, you could use a member like so

    class Sphere : public Shape
    {
        ...
    public: 
    
        boost::variant<scope::Void, scope::Point, scope::Circle>
            intersect_type;
    
        intersect_type intersect(const Plane& P);
    
        ...
    };
    

    so that we can use

    Sphere::intersect_type t = S.intersect(P);
    

    where S is an instance of Sphere and P an instance of Plane. But then still we’d have to have separate handling of all possible types:

    if (intersection.isPoint()){
        ...
    }
    else if (intersection.isCircle()){
        intersection.radius;
    }
    // etc.
    

so that the complexity we tried to take away from the user is actually still there.

I feel like I’m missing something here. Perhaps there is a smarter way to implement my Shape baseclass? Or should I create a separate, dedicated Intersect class? What solution is most elegant, efficient and effective for this situation?

  • 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-12T09:39:48+00:00Added an answer on June 12, 2026 at 9:39 am

    Off-hand:

    The isXXXX() predicate methods seem like a code smell to me. You’d do

    • if (dynamic_cast<Circle*>(shapePtr)) with RTTI usually
    • Or use variant::which() and/or variant::type() to discriminate the variant’s stored value

    To your question:

    There are several possible approaches.

    1. The classic OO approach would be to just derive everything from Shape and always return a std::unique_ptr<Shape> (or similar).

    2. However, obviously, you can do modern C++ static OO, in which case you’d end up with something similar to the variant. You’d then write a visitor to handle different cases:

    (live on http://liveworkspace.org/code/bad329cb40d94a21531e1153f4c0877b)

    #include <string>
    #include <iostream>
    #include <boost/lexical_cast.hpp>
    #include <boost/variant.hpp>
    #include <boost/variant/static_visitor.hpp>
    
    struct Shape 
    { 
        /*virtual*/ double getSurface() const { return 42.0; }  // TODO
    };
    
    struct Circle : Shape {};
    struct Point : Shape {};
    struct Rect : Shape {};
    
    struct Nil {};
    
    typedef boost::variant<Nil, Circle, Point, Rect> Intersect;
    
    struct DescribeVisitor : boost::static_visitor<std::string>
    {
        std::string operator()(Circle const& s) const {
            return std::string("Got a circle of ") + boost::lexical_cast<std::string>(s.getSurface());
        }
    
        std::string operator()(Rect const& s) const {
            return std::string("Got a rectangle of ") + boost::lexical_cast<std::string>(s.getSurface());
        }
    
        std::string operator()(Point const& s) const {
            return std::string("Got a point of ") + boost::lexical_cast<std::string>(s.getSurface()); // mmm bit funny :)
        }
    
        std::string operator()(Nil const&) const {
            return std::string("Got an empty intersection");
        }
    };
    
    std::ostream& operator<<(std::ostream& os, Intersect const& i)
    {
        return os << boost::apply_visitor(DescribeVisitor(), i);
    }
    
    int main(int argc, const char *argv[])
    {
        Intersect describe = Point();
        std::cout << describe << std::endl;
    
        describe = Rect();
        std::cout << describe << std::endl;
    
        describe = Circle();
        std::cout << describe << std::endl;
    }
    

    Output:

    Got a point of 42
    Got a rectangle of 42
    Got a circle of 42
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have something like this: class A { public B mem; public int
Suppose I have a class like the following: public class Stage { public int
Suppose I have something like the following: class Point : geometry { ... Point(double
Suppose I have the following class: public class TestBase { public bool runMethod1 {
Suppose I have something like the following in test.cxx (and that I do the
Suppose you have something like this: class intlist: def __init__(self,l = []): self.l =
Suppose I have the following class: public class MyClass { public decimal myDecimal; public
Let's suppose that I have the following class which tries to be immutable public
Suppose I have the following class: public class FixExpr { Expr<FixExpr> in; } Now
Suppose I have something like this page: <noscript>You need JS for this page</noscript> <script>

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.