由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Question about type conversion
相关主题
也来一道c++的题用C设计Stack的interface,要求支持各种数据类型。
问个简单的C++ 函数参数问题怎么检查极端情况?
bloomberg电面2,攒rp求bless (给据了 :()讨论一道题:找出一个board上的所有单词
one c++ question有简洁树代码么
C++ online Test 又一题问一个C++的binary search tree类实现问题 (转载)
问一个C++ set和unordered_set iterator的问题How to find the size of an array? Thanks.
什么情况下pass by reference比pass by pointer好?recursive中该用reference 还是普通变量传递?
请问strcpy()和memcpy()的写法问题leetcode上的populate next node I and II
相关话题的讨论汇总
话题: foo话题: bar话题: void话题: class话题: question
进入JobHunting版参与讨论
1 (共1页)
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 )
: {

1 (共1页)
进入JobHunting版参与讨论
相关主题
leetcode上的populate next node I and IIC++ online Test 又一题
srand()的问题问一个C++ set和unordered_set iterator的问题
报个电面的面经和据信吧, 求安慰什么情况下pass by reference比pass by pointer好?
leetcode上的sorted list to BST请问strcpy()和memcpy()的写法问题
也来一道c++的题用C设计Stack的interface,要求支持各种数据类型。
问个简单的C++ 函数参数问题怎么检查极端情况?
bloomberg电面2,攒rp求bless (给据了 :()讨论一道题:找出一个board上的所有单词
one c++ question有简洁树代码么
相关话题的讨论汇总
话题: foo话题: bar话题: void话题: class话题: question