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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:21:47+00:00 2026-06-03T09:21:47+00:00

I have a base class, Primitive , from which I derive several other classes–

  • 0

I have a base class, Primitive, from which I derive several other classes–Sphere, Plane, etc.

Primitive enforces some functionality, e.g intersect(), on its subclasses through pure virtual functions. The computation of intersect depends on instance data, so it makes sense to have it as a member method.

My problem arises in the following:
I want every derived instance to be able to identify its type, say through a std::string type() member method. As all instances of the same class will return the same type, it makes sense to make type() a static method. As I also want every Primitive subclass to implement this method, I would also like to make it a pure virtual function, like intersect() above.

However, static virtual methods are not allowed in C++.
C++ static virtual members?
and
Can we have a virtual static method ? (c++)
ask similar questions but they do not include the requirement of enforcing the function on derived classes.

Can anyone help me with the above?

  • 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-03T09:21:49+00:00Added an answer on June 3, 2026 at 9:21 am

    You could have a non-static virtual method calling the static one (or returning a static string), appropriately implemented in each derived class.

    #include <iostream>
    #include <string>
    
    struct IShape {
      virtual const std::string& type() const =0;
    };
    
    struct Square : virtual public IShape {
      virtual const std::string& type() const { return type_; }
      static std::string type_;
    };
    std::string Square::type_("square");
    
    int main() {
    
      IShape* shape = new Square;
      std::cout << shape->type() << "\n";
    
    }
    

    Note that you will have to implement the type() method for every subclass anyway, so the best you can do is for the string to be static. However, you may consider using an enum instead of a string, do avoid unnecessary string comparisons in your code.

    Now, going back to the fundamentals of the problem, I think the design is somewhat flawed. You cannot really have a general intersection function that operates on all kinds of shapes, because the types of shapes resulting from intersections vary greatly, even for the same type of shape (two planes can intersect in a plane, a line, or not intersect at all, for example). So in trying to provide a general solution, you will find yourself performing these kind of type checks all over the place, and this will grow unmaintainably the more shapes you add.

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

Sidebar

Related Questions

I have a Base Class, called primitive Graphics. derived from this class are several
I have a base class vehicle and some children classes like car, motorbike etc..
i have base class and several derived classes. how can i resolve these derived
I have a base class called BaseEvent and several descendants classes: public class BaseEvent
I have base-class Base from which is derived Derived1 , Derived2 and Derived3 .
I have a base class with several derived classes that extend it. I want
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have an interface with several events I have base class implementing the interface
I have a base class ReportElement which has type property: public abstract class ReportElement
I have a base class object array into which I have typecasted many different

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.