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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:03:25+00:00 2026-06-01T16:03:25+00:00

Consider the problem of getting an object as argument and printing its type: #include

  • 0

Consider the problem of getting an object as argument and printing its type:

#include <iostream>

class A { };
class B : public A { };
class C : public A { };
class D : public C, public B { };

using namespace std;

template<class T>
void print_type(T* info)
{
    if(dynamic_cast<D*>(info))
        cout << "D" << endl;
    else if(dynamic_cast<C*> (info))
        cout << "C" << endl;
    else if(dynamic_cast<B*>(info))
        cout << "B" << endl;
    else if(dynamic_cast<A*> (info))
        cout << "A" << endl;
}

int main(int argc, char** argv)
{
    D d;
    print_type(&d);
    return 0;
}

It gives me the following error: “Ambiguous conversion from derived class ‘D’ to base class.”
But I fail to see where’s the ambiguity: if the object declared in main (d) is of type D, why can’t be it directly converted to a type A?

Also, if I pass an argument of type string of course I get other errors:
'std::basic_string<char>' is not polymorphic

In Java for generics there is the syntax: <T extends A>; in this case it would be useful. How can I make a similar thing in C++ with templates?


I have modified the code this way:

#include <iostream>
#include <vector>

class A { };
class B : virtual public A { };
class C : virtual public A { };
class D : public C, public B { };

using namespace std;

template<class T>
void print_type(T* info)
{
    if(dynamic_cast<D*>(info))
        cout << "D" << endl;
    else if(dynamic_cast<C*> (info))
        cout << "C" << endl;
    else if(dynamic_cast<B*>(info))
        cout << "B" << endl;
    else if(dynamic_cast<A*> (info))
        cout << "A" << endl;
}

int main(int argc, char** argv)
{
    string str;
    print_type(&str);
    return 0;
}

But I still get the error: 'std::basic_string<char>' is not polymorphic

  • 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-01T16:03:26+00:00Added an answer on June 1, 2026 at 4:03 pm

    Consider the problem of getting an object as argument and printing it’s type:

    Sigh… use RTTI.

    #include <iostream>
    #include <string>
    #include <typeinfo>
    
    template<class T> void print_type(const T& info){
        std::cout << typeid(info).name() << std::endl;
    }
    
    int main(int argc, char** argv){
        D d;
        int a = 3;
        std::string test("test");
        print_type(d);
        print_type(a);
        print_type(test);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Propose to consider the following problem. Suppose we have some composite object. Are there
I have tried to search on HashMap in Android, but getting problem: Consider this
Consider the following example: #include <iostream> #include <sstream> #include <vector> #include <wchar.h> #include <stdlib.h>
Consider: static class EntranceClass { public: static void RegisterSomething() { } static int main()
Consider this code: TimeStamp.Text = BlogComment.Date.UtcNow.ToString(yyyy-MM-ddTHH\:mm\:ss.fffffffzzz); BlogComment.Date is a DateTime object with its date
Consider this problem: There's a square grid defined, each tile being either passable (1)
In Algorithm Design Manual , there is such an excise 4-26 Consider the problem
Consider the following problem: A multi-line string $junk contains some lines which are encoded
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
This problem is only in IE. Consider the following HTML: <html> <body> <div style='position:absolute;left:1em;right:1em;top:1em;bottom:1em;overflow:auto;>

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.