C***y 发帖数: 2546 | 1 Got error C2664: 'foo' : cannot convert parameter 1 from 'B *' to 'A *&'
How can I handle it? Thanks!
class A
{
};
class B: public A
{
};
void foo( A*& a )
{
}
void bar( B*& b )
{
foo(b);
}
int main()
{
B* b = NULL;
bar( b);
return 0;
} |
C***y 发帖数: 2546 | 2 I can't change the type of argument |
r****t 发帖数: 10904 | 3 can you change the function body of foo/bar? ptr is not class obj, having no
polymorphism.
【在 C***y 的大作中提到】 : I can't change the type of argument
|
f*******n 发帖数: 12623 | 4 You can't. B*& is not compatible with A*&, similar to how B** is not
compatible with A**. If you could, you could do terrible things in foo()
like assign an A* to the variable that's supposed to be B*.
Maybe if you made a reference to const it would be okay, but I'm not sure about this (don't remember exactly). |
b***i 发帖数: 3043 | 5 改成
class A{
public:
int a;
A():a(0){};
};
class B: public A{
};
void foo( A*& a ){
a->a=1;
}
void bar( B*& b ){
foo((A*&)b);
}
int main(){
B* b = new B();
bar( b);
cout<<(b->a);
return 0;
}
【在 C***y 的大作中提到】 : Got error C2664: 'foo' : cannot convert parameter 1 from 'B *' to 'A *&' : How can I handle it? Thanks! : class A : { : }; : class B: public A : { : }; : void foo( A*& a ) : {
|