C++之嵌套内部类用法(七十五)
C++ 用法 内部 嵌套
2023-09-14 09:09:57 时间
1.代码示例
1.test.h
#include <iostream>
using namespace std;
//父类
class Person{
public:
int num;
void foo();
//静态变量
static string mSelf;
static int add(int x, int y);
//内部类
class Student{
public:
int num;
void foo();
static string mSelf;
static int add(int x, int y);
} stu;
};
2.test.cpp
#include "inner.h"
void Person::foo(){
num = 11111;
cout << " Parent Class method..." << endl;
}
void Person::Student::foo(){
num = 22222;
cout << " Son Class method..." << endl;
}
//错误用法
#if 0
static int Person::add(int x, int y){
cout <<__FUNCTION__<<"(), line = " <<__LINE__<<", x + y = " << x + y << endl;
}
static int Person::Student::add(int x, int y){
cout <<__FUNCTION__<<"(), line = " <<__LINE__<<", x + y = " << x + y << endl;
}
#endif
//正确用法
int Person::add(int x, int y){
cout <<__FUNCTION__<<"(), line = " <<__LINE__<<", x + y = " << x + y << endl;
}
int Person::Student::add(int x, int y){
cout <<__FUNCTION__<<"(), line = " <<__LINE__<<", x + y = " << x + y << endl;
}
//错误用法
//static string Person::mSelf = "I in Person Class";
//static string Person::Student::mSelf = "I in Son Class";
//正确用法
string Person::mSelf = "I in Person Class";
string Person::Student::mSelf = "I in Son Class";
int main()
{
Person per;
Person *ppp = new Person;
per.foo();
ppp->add(111, 111);
Person::add(123,456);
cout << Person::mSelf << endl;
cout << per.num << endl << endl;
cout << "**************************************" << endl;
per.stu.foo();
per.stu.add(1,2);
Person::Student::add(2222,33333);
cout << Person::Student::mSelf << endl;
cout << per.stu.num << endl;
return 0;
}
相关文章
- C++中this指针的作用以及用法详解
- C++丨数据类型基础
- windows下安装MinGW及C++的环境配置
- c++ CreateThread
- 10min快速回顾C++语法(二)
- C++运算符重载(五)之关系运算符重载
- C++ CLR Nibble 格式的数据存储
- Android通过jni调用本地c/c++接口方法总结
- 【说站】高性能 C++ HTTP客户端的原理
- 深入理解C++11_c++ string char
- c++ 中map 的find 用法[通俗易懂]
- c++语言switch用法举例_switch语句特点
- C++笔记
- 论c++中的数组,vector和array的区别及用法
- 【超详细】*和&在C/C++中的常见用法(附示例讲解)
- C/C++ CreateFileMapping 共享内存
- C++: 深拷贝的传统用法和现代用法
- C/C++中对链表操作的理解&&实例分析详解编程语言
- C++ uniform_int_distribution离散均匀分布类用法详解
- C++ forward_list用法详解
- C++ stack(STL stack)用法详解
- C++ piecewise_linear_distribution分段线性分布随机数用法详解
- C++ gslice类用法详解
- C++ getline函数用法详解
- 浅析C++的特殊工具与技术
- C/C++中可变参数的用法详细解析
- C++中引用(&)的用法与应用实例分析
- C++中const的用法详细总结
- c++连接mysql数据库的两种方法(ADO连接和mysqlapi连接)
- c++作用域运算符用法(全局变量和局部变量)
- C++实现二叉树非递归遍历方法实例总结
- C++位操作的常见用法小结