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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:30:19+00:00 2026-05-26T00:30:19+00:00

Having class TaskBase , each derived class of it must have name and unique

  • 0

Having class TaskBase, each derived class of it must have name and unique id.

The TaskBase is something like below:

class TaskBase
{
public:
    static const int id()
    {
        // return an unique id, for each object or derived class, HOW ??
    }

    static const string name()
    {
        // return class name for each derived class, HOW ??
        // for example : "TaskBase" for this class
    }
};

My try was :

template <typename DERIVED>
class TaskBase
{
public:

    static const int id() 
    {
        static const int id = reinterpret_cast<int> (typeid (DERIVED).name());
        return id;
    }

    static const string name() 
    {
        static string n;

        if (!n.size())
        {
            int status;
            char *realname = abi::__cxa_demangle(typeid (DERIVED).name(), 0, 0, &status);
            n = realname;
            free(realname);
        }

        return n;
    }
};

I already read this, but i need the ability to have base pointer to each derived classes, something line below:

class MyTask1 : public TaskBase
{
};

MyTask1 myTask1, myTask2;
TaskBase *base = &myTask1;
  • 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-26T00:30:19+00:00Added an answer on May 26, 2026 at 12:30 am
    class TaskBase
    {
    private:
        const void*  m_id;
        string m_name;
    
    public:
        TaskBase(const void* m_id, string m_name): m_id(m_id), m_name(m_name)
        {
        }
    
        const void* id() const
        {
            return m_id;
        }
    
        string name() const
        {
             return m_name;
        };
    };
    
    template< typename DERIVED >
    class TaskProxy: public TaskBase
    {
    public:   
        static const void* id()
        {
            //if you want to have for each object a unique id:
            //return reinterpret_cast<void*>(this);
            //just for each TaskProxy<????>:
            return reinterpret_cast<const void*>(typeid( DERIVED ).name());
        }
    
        static string name()
        {
            return typeid( DERIVED ).name();
        }
    
        TaskProxy(): TaskBase(id(), name()) {}
    };
    

    Usage:

    class MyTask1 : public TaskProxy< MyTask1 >
    {
    };
    
    class MyTask2 : public TaskProxy< MyTask2 >
    {
    };
    
    ...
    
    MyTask1 myTask1;
    TaskBase *baseA = &myTask1;
    MyTask2 myTask2;
    TaskBase *baseB = &myTask2;
    
    cout << "Name: " << baseA->name() << "  Id:" << baseA->id() << endl;
    cout << "Name: " << baseB->name() << "  Id:" << baseB->id() << endl;
    

    Which outputs this (with gcc 4.6):

    Name: 7MyTask1  Id:0x401228
    Name: 7MyTask2  Id:0x4011c0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Comment class having properties and their methods like public Comment
Having the following class (.Net 3.5): public class Something { public string Text {get;
Having a class like this: class Spam(object): def __init__(self, name=''): self.name = name eggs
I have div having class name .modalPopup inside the div I have multiple table
I have two .jar files having same package structure and same class name. Eg.
I have some div elements having class name hover , these div elements have
Having the following class: public class SomeClass { private readonly int[] _someThings; public SomeClass()
I have a div having a css class MyClass. Inside this div, I can
Does PHP have a method of having auto-generated class variables? I think I've seen
I am having a class called Customer. Customer *object; //in this object i have

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.