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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:30:40+00:00 2026-05-13T05:30:40+00:00

My C++ is a bit rusty. Here’s what I’m attempting to do: class Cmd

  • 0

My C++ is a bit rusty. Here’s what I’m attempting to do:

class Cmd { };
class CmdA : public Cmd { };
class CmdB : public Cmd { };
...
Cmd *a = new CmdA ();
Cmd *b = new CmdB ();

First problem:

cout << typeid (a).name ()
cout << typeid (b).name ()

both return Cmd * types. My desired result is CmdA* and CmdB*. Any
way of accomplishing this other than:

if (dynamic_cast <CmdA *> (a)) ...

Second, I would like to do something like this:

class Target {
    public:
        void handleCommand (Cmd *c) { cout << "generic command..." }
        void handleCommand (CmdA *a) { cout << "Cmd A"; }
        void handleCommand (CmdB *b) { cout << "Cmd B"; }
};

Target t;
t.handleCommand (a);
t.handleCommand (b);

and get the output “Cmd A” and “Cmd B”. Right now it prints out
“generic command…” twice.

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-13T05:30:41+00:00Added an answer on May 13, 2026 at 5:30 am

    Ah but typeid(a).name() will be Cmd* because its defined as Cmd*. typeid(*a).name() should return CmdA

    http://en.wikipedia.org/wiki/Typeid

    Also, the base class of whatever you pass to typeid must have virtual functions, otherwise you get back the base class.

    MSDN has a more eloquent explanation for that:

    If the expression points to a base
    class type, yet the object is actually
    of a type derived from that base
    class, a type_info reference for the
    derived class is the result. The
    expression must point to a polymorphic
    type (a class with virtual functions).
    Otherwise, the result is the type_info
    for the static class referred to in
    the expression. Further, the pointer
    must be dereferenced so that the
    object it points to is used. Without
    dereferencing the pointer, the result
    will be the type_info for the pointer,
    not what it points to.

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

Sidebar

Ask A Question

Stats

  • Questions 241k
  • Answers 241k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should take a memory dump of your program at… May 13, 2026 at 7:28 am
  • Editorial Team
    Editorial Team added an answer Yes, it is safe, if enctype of the form is… May 13, 2026 at 7:28 am
  • Editorial Team
    Editorial Team added an answer Easy workaround - use another diff tool. I'm serious. I… May 13, 2026 at 7:28 am

Related Questions

My C++ is a bit rusty. Here's what I'm attempting to do: class Cmd
I am writing a small matrix library in C++ for matrix operations. However, my
My C++ is a bit rusty so... #include<list> typedef list<int> foo; that gives me
I'm a bit rusty, actually really rusty with my C++. Haven't touched it since
I had this idea of creating a count down timer, like 01:02, on the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.