English 中文(简体)
动态转换
原标题:Ambiguous conversion in dynamic_cast

2. 考虑将物体作为论据和印刷其类型的问题:

#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

在 Java,有异构体:<Text A>;在这种情况下是有用的。 我如何在C++中用模板做类似的事情?


我这样修改了法典:

#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;
}

但我仍然有错误:<>>>>。 st st bas ; ; ;

最佳回答

2. 考虑将物体作为论据并印刷其类型的问题:

Sigh... 使用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;
}
问题回答

首先,这不是一个模板问题。 如果您删除该模板,而仅仅有<条码>印本——类型。 页: 1

正在发生的是你而不是使用虚拟继承财产,因此,你取得了以下结果:

A   A
|   | 
B   C
  /
  D

动态版面指,>>

为此(我假定这是你想要的)。

  A
 / 
B   C
  /
  D

...you should use virtual inheritance, ergo:

class A
{
};

class B : virtual public A
{
};

class C : virtual public A
{
};

class D : public C,public B
{
};

......现在,它汇编了无问题: (铭记虚拟遗产是陈词典)

http://en.wikipedia.org/wiki/Diamond_problem”rel=“nofollow”deadly Diamond of death,或简单地说,是钻石问题。 对A的“pa”可以穿过B或C,从而产生潜在的矛盾。

此外,模板的想法是使其具有通用性,而不是型号。 模板本身并不编纂成法典,而是用其使用加以汇编。 它有很多东西像一个大的宏观。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?