C++继承vector并重载operator[] 内部类访问外部类操作
const int num = 10;
template<typename Type>
class myVector :public std::vector<Type>
{
public:
Type& operator[](const int i);
};
template<typename Type>
Type &myVector<Type>::operator[](const int i)
{
myTest();
return *(std::vector<Type>::begin() + (i + num));
}
内部类访问外部类
#include <iostream>
using namespace std;
class outclass
{
public:
int m;
class inclass//内部类通过parent指针访问外部类的成员,包括public、private
{
public:
void set_m(int i)
{
outclass *parent = (outclass *) ((char*)this - offsetof(outclass,in));
parent->m = i;
}
}in; // 这里需要生命类变量作为数据成员存在
};
int main()
{
outclass out;
out.in.set_m(123);
cout << out.m << endl;
return 0;
}