opt*_*nal 4 c++ generics pointers vector
我很难找到有关这些东西的信息!:(
我很困惑为什么这不起作用:
vector<B*> b;
vector<C*> c;
(B and C are subclasses of A)
(both are also initialized and contain elements etc etc...)
template <class First, class Second>
bool func(vector<First*>* vector1, vector<Second*>* vector2)
return vector1 == vector2;
Run Code Online (Sandbox Code Playgroud)
编译时返回:
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这不起作用,指针持有地址是啊?那么为什么不只是比较两个向量指针是否指向相同的地址(-es)?
这是一个简单的例子,你要求的东西不起作用.
struct A{ int i; };
struct OhNoes { double d; };
struct B: public A {};
struct C: public OhNoes, public B {};
Run Code Online (Sandbox Code Playgroud)
所以这里,B和C都是A的子类.但是,一个实例C不太可能与其B子对象具有相同的地址.
就是这样:
C c;
B *b = &c; // valid upcast
assert(static_cast<void*>(b) == static_cast<void *>(&c));
Run Code Online (Sandbox Code Playgroud)
将失败.