woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

基类子类的this指针相同,但在基类函数永远调用基类的成员变量

// ttttest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
class Base
{
public:
Base()
{
   a = 10;
   b = 9;
}
virtual void Cry()
{
 
   Shout();
}

void Shout()
{
   printf("%d%d",a,b);
   printf("%d%d",this->a,this->b);
//
基类子类的this指针相同,但在这里this->a,this->b里的a,b都是基类的成员变量,不过基类和子类共用一个b
//
成员,所以b 是在子类里改过的。
//
父类中任何成员函数体内直接调用的函数,绝不可能是子类的。
}
protected:
int a;
int b;
private:
};

class Derived:public Base
{
public:
int a;
Derived()
{
   a = 11;
   b = 12;
}
void Cry()
{
   printf("%d%d",this->a,this->b);
   Shout();
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Base * p = new Derived;
p->Cry();
delete p;
return 0;
}

 

posted on 2009-08-28 14:44 肥仔 阅读(806) 评论(0)  编辑 收藏 引用 所属分类: C++ 基础


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