日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++内部类访问外部类

發(fā)布時間:2024/4/17 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++内部类访问外部类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

采用如下的方式訪問外部類:

// OperatorTest.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。
//

#include <iostream>
#include<vector>
using namespace std;
class Obj {
?? ?static int i, j;
public:
?? ?void f() {
?? ??? ?cout << "i:" << i++ << endl;
?? ?}
?? ?void g() {
?? ??? ?cout << "j:" << j++ << endl;
?? ?}
};
int Obj::i = 11;
int Obj::j = 12;

class ObjContainer {
?? ?vector<Obj*> objs;
public:
?? ?class SmartPointer;
?? ?friend class SmartPointer;
?? ?void add(Obj* obj) {
?? ??? ?objs.push_back(obj);
?? ?}
?? ?class SmartPointer {
?? ??? ?ObjContainer& oc;
?? ??? ?int index;
?? ?public:
?? ??? ?SmartPointer(ObjContainer& oc):oc(oc){
?? ??? ??? ?index = 0;
?? ??? ?}
?? ??? ?bool operator++() {
?? ??? ??? ?index++;
?? ??? ??? ?if (index >=oc.objs.size())return false;
?? ??? ??? ?if (oc.objs[index] == 0)return false;
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?bool operator++(int) {
?? ??? ??? ?return operator++();
?? ??? ?}
?? ??? ?Obj* operator->() {
?? ??? ??? ?return oc.objs[index];
?? ??? ?}
?? ?};
?? ?SmartPointer begin() {
?? ??? ?return SmartPointer(*this);
?? ?}
};
int main()
{
?? ?const int sz = 12;
?? ?ObjContainer oc;
?? ?Obj objs[sz];
?? ?ObjContainer::SmartPointer sp = oc.begin();
?? ?for (int i = 0; i < sz; i++) {
?? ??? ?oc.add(&objs[i]);
?? ?}
?? ?do {
?? ??? ?sp->f();
?? ??? ?sp->g();
?? ?} while (sp++);
}
?

總結(jié)

以上是生活随笔為你收集整理的C++内部类访问外部类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。