• 全部
  • 问答

已知子类,怎样取得父类?

VincintCao 2002-12-03 11:06:20
class A;
class B : public class A;
B b;
怎样取得A的实体?最好不要用强制转换。
我用dynamic_cast来做,但是,dynamic_cast不能将const class A *const转换成class A*。但是我觉得应该有办法可以实现的。
...全文
29 点赞 收藏 12
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
VincintCao 2002-12-05
谢谢
:)
回复
yarshray 2002-12-04
class C :public A, public B;
这种时候通常采取虚拟继承

class A : virtual public Base {}
class B : virtual public Base {}

class Child : public A,B{}

更多关于虚拟继承

http://www.156ok.com/article/article_list.asp?account_id=430
回复
pick 2002-12-04
先定义一个A的指针 p
再用b对它赋值
回复
ablist 2002-12-04
我使用重载操作符 = 实现对象考贝, 不可实现你的要求
class CBase;
class CDivide;

#include <iostream.h>

class CBase
{
public:
virtual void Draw();
CBase();
virtual ~CBase();
CBase& operator =(CDivide& rhs);

private:
int m_nData;

friend class CDivide;
friend class CBase; //=操作符函数不会出错,我也知道为什么
};


class CDivide : public CBase
{
public:
void SetBaseData(int nData);
CDivide();
virtual ~CDivide();

private:
int m_nData; //非CBase::m_nData

//friend class CBase;
};

CBase::CBase() : m_nData(5)
{

}

CBase::~CBase()
{

}

CBase& CBase::operator =(CDivide& rhs)
{
this->m_nData = rhs.CBase::m_nData;
return (*this);
}
CDivide::CDivide() : m_nData(10)
{

}

CDivide::~CDivide()
{

}

void CBase::Draw()
{
cout << "The Base class data : " << this->m_nData << endl;
}

void CDivide::SetBaseData(int nData)
{
this->CBase::m_nData = nData;
}

int main()
{
// Class test

cout << sizeof(CBase) << endl;
cout << sizeof(CDivide) << endl;
CDivide div;
div.Draw();
div.SetBaseData(15);
div.Draw();
CBase base;
base.Draw();
base = div;
base.Draw();
return 0;
}
回复
mr_oydy 2002-12-04
//你说的是映射问题.

const C AnotherInstC = C(); //const对象要显式初始化
printf( "InstancC's addr:%p\n", &InstanceC );
printf( "BaseA part of InstancC's addr:%p\n",dynamic_cast<const BaseA*>(&AnotherInstC) );
printf( "BaseB part of InstancC's addr:%p\n", dynamic_cast<const BaseB*>(&AnotherInstC) );
回复
VincintCao 2002-12-04
由于我表达不清,引起了较多歧义,我写了一段代码:
#include <stdio.h>

class BaseA
{
public:
const BaseA& operator=(int i)
{ this->m_a = i; return *this; }
public:
int m_a;
};

class BaseB
{
public:
int m_b;
};

class C: public BaseB, public BaseA
{
public:
int m_c;
};

int main()
{
BaseA InstanceA;
C InstanceC;
InstanceA = 9;
//InstanceC = 9;// ERROR: Koshima(RedApple) ( ) 信誉:100
//BaseA(InstanceC) = 9;// ERROR: zhf0021(屡战屡败,屡败屡战) ( ) 信誉:100
BaseA *pBaseA;
pBaseA = &InstanceC;
*pBaseA = 8;// OK: pick() ( ) 信誉:100 , but......

//---------------------------------------------------------------------------------------------//
printf( "InstancC's addr:%p\n", &InstanceC );
printf( "BaseA part of InstancC's addr:%p\n",dynamic_cast<BaseA*>(&InstanceC) );
printf( "BaseB part of InstancC's addr:%p\n",dynamic_cast<BaseB*>(&InstanceC) );

//const C AnotherInstC;
//printf( "InstancC's addr:%p\n", &InstanceC );
//printf( "BaseA part of InstancC's addr:%p\n",dynamic_cast<BaseA*>(&AnotherInstC) );
//printf( "BaseB part of InstancC's addr:%p\n", dynamic_cast<BaseB*>(&AnotherInstC) );
//---------------------------------------------------------------------------------------------//
return 0;
}
我的疑惑在于后半部分:const的C对象难道就没有办法取得他的BaseA部分吗?
回复
solar 2002-12-03
rtti
回复
zhf0021 2002-12-03
#include "iostream.h"

class a
{
public:
void f()
{
cout<<"a"<<endl;
}
};
class b:public a
{
public:
void f()
{
cout<<"b"<<endl;
}
};
int main(int argc, char* argv[])
{
b bb;
bb.f();
a(bb).f();


return 0;
}
回复
artman 2002-12-03
强制转换
回复
VincintCao 2002-12-03
请不要用强制转换的方法!
class A;
class B;
class C :public A, public B;
请考虑强制转换的危险性。
回复
VincintCao 2002-12-03
To: artman(我变,变,变) & zhf0021(屡战屡败,屡败屡战) & Koshima(RedApple)
A已经重载了=运算符,所以
A a;
a = ...
这时候用
B b
b = ...
能行吗?

To:solar(这里空间太小!!!)
可否解释详细
回复
Koshima 2002-12-03
在使用A的对象的地方用B的对象来代替,绝对不会出错,何必要转换?(指针也如此)
回复
发帖
C++ 语言
创建于2007-09-28

5.9w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2002-12-03 11:06
社区公告
暂无公告