子类对象如何访问父类的成员变量?

cumirror 2010-06-20 05:15:14

#include <iostream>
using namespace std;

class Father{
public:
int a;
Father(int x){}
};

class Child:public Father{
public:
Child(int x):Father(x-1){
a=x;
}
};

int main(){
Child test(5);
cout<<test.a<<endl;
Father* x=static_cast<Father*>(&test);
cout<<x->a<<endl;
return 0;
}

test对象中_a=5;但其父类中_a=4,可以用何种方法得到Father中_a的值?
我尝试用向上转型static_cast但是不行。

希望高手能点播下,谢谢了。
...全文
4225 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
tjyjx7946358 2010-06-21
  • 打赏
  • 举报
回复
我感觉因为a是父类的public,所以子类跟父类共享数据空间,因而a不会变。
zglin689 2010-06-21
  • 打赏
  • 举报
回复
不是很明白意思,,
maoloverme1 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 michaelbomb 的回复:]
C/C++ code
test.Father::a;
[/Quote]
这也不行,应该取不到。
cumirror 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 gkathere 的回复:]

C/C++ code
#include "stdafx.h"
#include <iostream>
using namespace std;


class Father{
public:
int a;
Father(int x){a = x;} ……
[/Quote]

谢谢,通过变量同名可以解决这个问题,另外在网上搜索相关的资料时有了更大的收获:
创建子对象时会先调用父类的构造函数再调用子类的构造函数,若在child中没有同名成员变量时,它是与Father共享一份代码,于是a先在父类中赋值为4,再在子类中又赋值为5;
测试代码如下:
#include <iostream>
using namespace std;


class Father{
public:
int a;
Father(int x){
a = x;
cout<<a<<endl;
}
void display(){
cout<<a<<endl;
}
};

class Child:public Father{
public:
// int a ; //同名变量
Child(int x):Father(x-1){
cout<<a<<endl;
}
};
int main()
{
Child test(5);
return 0;
}

输出是:4
4


当采用同名变量后,成员变量a的代码不会共享,可以获得不同的值。
为了说明代码共享请看下面的示例:
#include <iostream>
using namespace std;


class Father{
public:
int a;
Father(int x){
a = x;
cout<<a<<endl;
}
void display(){
a+=10;
cout<<a<<endl;
}
};

class Child:public Father{
public:
int a ; //增加变量
Child(int x):Father(x-1){
a=x;
cout<<a<<endl;
}
};
int main()
{
Child test(5);
cout<<test.a<<endl;
//c此处调用的是共享代码区间,故display中访问的是父类的成员变量a,子类成员a不变
test.display(); //输出的是父类变量a
Father* x=static_cast<Father*>(&test);
cout<<x->a<<endl; //与前面的结果想验证
cout<<test.a<<endl; //子类中的成员a并没有改变
return 0;
}

输出是:
4(父类构造函数)
5(子类构造函数)
5(子类成员变量)
14(display中修改的是父类成员变量)
14
5(子类成员变量未变)
向立天 2010-06-21
  • 打赏
  • 举报
回复
你是不是要实现什么需求
还是直说要干什么吧
YHL27 2010-06-21
  • 打赏
  • 举报
回复
学习。。。
GKatHere 2010-06-20
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
using namespace std;


class Father{
public:
int a;
Father(int x){a = x;} //构造函数
};

class Child:public Father{
public:
int a ; //增加变量
Child(int x):Father(x-1){
a=x;
}
};
int main()
{
Child test(5);
cout<<test.a<<endl;
Father* x=static_cast<Father*>(&test);
cout<<x->a<<endl;
return 0;

}


输入5,4,
俺再也不说什么了
MichaelBomb 2010-06-20
  • 打赏
  • 举报
回复
test.Father::a;
MichaelBomb 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 cumirror 的回复:]
引用 10 楼 xiaozhu8766 的回复:
Father::a


a不是静态变量,这样不行。
[/Quote]

不是静态变量 的话 也可以吧
largep 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 pengzhixi 的回复:]

说了父类构造函数什么都没做,所以一开始父类里面的a是一个随机值
[/Quote]

class Father{
public:
int a;
Father(int x):a(x)
{ //a = x;
//cout << x << a << endl;
}
~Father(){ cout << a << endl; }
};

class Child:public Father{
public:
Child(int x):Father(x-1){
a=x;
}
};



int main()
{
Child test(5);
cout<<test.a<<endl;
Father* x=static_cast<Father*>(&test);
cout<<x->a<<endl;

return 0;
}

在父类中初始化了结果也一样。是不是因为只有一个实例对象,内存本来就只有一份a。
cq_yanglin 2010-06-20
  • 打赏
  • 举报
回复
在基类中在加一个函数赛
pengzhixi 2010-06-20
  • 打赏
  • 举报
回复
说了父类构造函数什么都没做,所以一开始父类里面的a是一个随机值
cumirror 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oyster2008 的回复:]

Father(int x){},你这个构造函数做了什么?
[/Quote]

里面a=x;写掉了
cumirror 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 largep 的回复:]

用VS2005,改成Father(int x){a = x};经过debug发现,初始化时,father的a确实赋值是4,但是当子类又进行赋值之后,这个值就被修改成5了,最后father析构的时候,a的值还是5。
[/Quote]

是否父类调用构造函数时有一个临时对象,当子类进行构造函数调用时,这个临时对象就销毁了?
无法找到中间的那个临时对象,所以无法获得4.不知是这样不。
cumirror 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ysinchina 的回复:]

test对象中_a=5;但其父类中_a=4,可以用何种方法得到Father中_a的值?
我尝试用向上转型static_cast但是不行。
----------
什么是类?什么是对象?

类,一个抽象数据类型,定义了一组数据对象和应用于这些数据对象的函数和操作符的数据结构。
类实例,计算机内描述的某个具体客观对象。
[/Quote]

“但其父类中_a=4”这样表达确实有误。你说的概念也是正确的。
liutengfeigo 2010-06-20
  • 打赏
  • 举报
回复
那不是乱套了啊。
cumirror 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gkathere 的回复:]

C/C++ code
class Father{
public:
int a;
Father(int x){a = x;}
};

class Child:public Father{
public:
int a ;
Child(int x):Fath……
[/Quote]

父函数构造函数我没写全,但补上后main中输出的仍为5、5.
cumirror 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lthyxy 的回复:]

C/C++ code

#include <iostream>
using namespace std;

class Father{
public:
int a;
Father(int x):a(x){}
};

class Child:public Father{
……
[/Quote]

这样可以,但若在子类中不增加成员变量b呢?
cumirror 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xiaozhu8766 的回复:]
Father::a
[/Quote]

a不是静态变量,这样不行。
zhengjiankang 2010-06-20
  • 打赏
  • 举报
回复
父类的非private成员 就是子类的成员
加载更多回复(10)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧