大龙的博客

常用链接

统计

最新评论

object slicing

#include "stdafx.h"
#include <iostream>
using namespace std;
class CObject
{
public:
 void test(){ Serialize(); };
 virtual void Serialize() { cout << "CObject::Serialize() \n\n"; }
};

class CDocument : public CObject
{
public:
 CDocument()
 {
  int b = 0;
 }
 CDocument(CDocument &c)
 {
  int a = 0;
 }
 int m_data1;
 void func()
 {
  cout << "CDocument::func()" << endl;
  Serialize();
 }

 virtual void Serialize() { cout << "CDocument::Serialize() \n\n"; }
};

class CMyDoc : public CDocument
{
public:

 int m_data2;
 virtual void Serialize()
 {
  cout << "CMyDoc::Serialize() \n\n";
 }
};
//---------------------------------------------------------------
void main()
{
 CMyDoc mydoc;
 CMyDoc* pmydoc = new CMyDoc;

 cout << "#1 testing" << endl;
 mydoc.func();

 cout << "#2 testing" << endl;
 ((CDocument*)(&mydoc))->func();

 cout << "#3 testing" << endl;
 pmydoc->func();

 cout << "#4 testing" << endl;
((CDocument)mydoc).func();
}

 

posted on 2008-06-30 16:22 大龙 阅读(206) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理