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

The Archive Base Latest Questions

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

Imagine I have such class hierarchy: Base A : Base B : Base C

  • 0

Imagine I have such class hierarchy:

Base
A : Base
B : Base
C : B

I want to be able to retrive a string type from Base object (I need string, not enum). I want also to be able to compare object type to A type for example:

Object *object = new A();
if (object->type() == A::typename())
{
   //hooray!
}

For now I’m planing to add a static function to each class:

static string typename() {return "Different name for each class";}

and then I will have to reimplement Base function virtual string type() for every derived class:

A: virtual string type() {return typename();} //A::typename
B: virtual string type() {return typename();} //B::typename
...

I think such design looks ugly. Is there some better way to achieve my goal?

Why I need this:
I’m developing a game. There is a tile map. Each tile has an array of objects on it. Some objects can be placed over the others. So i want to check if it is allow to put the object at the specific tile. For example: if tile has object with type “pot” then the flower can be put there.

  • 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-31T12:12:42+00:00Added an answer on May 31, 2026 at 12:12 pm

    You can achieve the same thing with dynamic_cast. Your classes are polymorphic anyway.

    Note that this is at least a code smell. You shouldn’t need to find the actual type of classes in a well-thought design. What underlying problem are you trying to solve?

    Also, typename is a keyword in C++, you should name your method differently.

    EDIT: A possible better solution for this would be to have a list of pairs of objects that can be stacked, and have virtual methods:

    class Object
    {
       virtual bool canStack(const std::string& baseObject) = 0;
    };
    
    class Flower
    {
       virtual bool canStack(const std::string& baseObject)
       {
           if ( baseObject == "pot" ) 
               return true;
           return false;
       }
    };
    

    Now I see why you’d want the get name.

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

Sidebar

Related Questions

Imagine I have String in C#: I Don’t see ya.. I want to remove
In C#, I have a class hierarchy with a couple of abstract base classes
Imagine you have a derived class, where the base class is something you cannot
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
Imagine I have a property defined in global.asax. public List<string> Roles { get {
In my model I have an abstract User class, and multiple subclasses such as
Imagine you have an aspect: @Aspect public class MyAspect { @Pointcut(whatever) public void anyAnnotation()
I want to have a SelectAll function which takes in a few arguments (class,
Imagine I have the following code: class A { [UsefulAttribute(foo)] B var1; [UsefulAttribute(bar)] B
Imagine I have a class used to represent some trivial numerical data, like a

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.