confusing c++ 重写 与 重定义 记录1
2024-08-30 01:17:29
class parent {
public:
void f() { cout << "parent f()" << endl; }
void f(int i) { cout << "parent f(int i)" << endl; }
}; class child :public parent {
};
int main() { child a;
a.f();
a.f();
return ;
}
父类中发生了重载。
子类中如果没有定义名字冲突的成员函数,这时候,父类中的两个函数都可以访问。
class child2 :public parent {
public:
void f() {
cout << "child f()" << endl;
}
};
int main() { child2 a; parent *p = &a;
child2 *c = &a; a.f(); //child f()
a.parent::f(); //parent f()
a.parent::f(); //parent f(2) p->f(); // parent f()
p->f(); // parent f(2) c->f(); // child f()
// c->f(2); illegal return ;
}
有冲突的时候,a只能直接访问自己定义的这个。
父类中的f必须通过a.parent::f这样的方式访问。这三个是并存的。
加了virtual以后呢?
最新文章
- 自定义样式 实现文件控件input[type=&#39;file&#39;]
- Extjs插入图片
- [eclipse]改项目名称后tomcat连接问题解决方法
- Cocos2d-x 学习资料推荐
- C#实现的ReplaceFirst和ReplaceLast
- 通过Html5的postMessage和onMessage方法实现跨域跨文档请求访问
- java日期详解
- Cocos2d-swift V3.x 中的update方法
- iframe与主框架跨域相互访问方法
- Android.os.SystemClock
- minicom 安装 查看串口
- editplus来编写html
- Python Word2Vec使用训练好的模型生成词向量
- scheme 之门
- Spring注解方式配置说明
- POJ 2195 Going Home(费用流)
- 在iOS 7中使用storyboard(part 1)
- linux-android(任务处理)
- 马尔科夫毯(Markov Blanket)
- [Linux] 服务器镜像定时备份解决方案 crontab+rsync+flock
热门文章
- [UE4]通过IP地址加入游戏
- 做好平衡有多难?谈MMO的职业设计
- CRM 2016 设置字体颜色
- _mysql.c(42) : fatal error C1083: Cannot open include file: &#39;config-win.h&#39;:问题的解决
- 第11章 拾遗4:IPv6(2)_给计算机配置IPv6地址
- elasticsearch 外网访问9200端口访问
- Android 硬编码
- tips:Java中的switch的选择因子
- 网络爬虫2:使用crawler4j爬取网络内容
- 查看Selinux和关闭Selinux