#include "stdafx.h" #include<iostream> #include<cstring> using namespace std; class Clazz { public: float x, y; string msg; const float& operator [] (uint8_t i) const { return (&x)[i]; } float& operator [] (uint8_t i) { return (&x)[i]; } Clazz() {} Clazz(float x, float y, string msg) :x(x), y(y), msg(msg) {} }; int main(void) { Clazz clazz = Clazz(1.0f,2.0f,"hello world"); cout << "clazz.x: " << clazz.x << endl; cout << "clazz.y: " << clazz.y << endl; cout << "clazz[0] " << clazz[0] << endl; cout << "clazz[1] " << clazz[1] << endl; cout << "clazz[2] " << clazz[2] << endl; cin.get(); return 0;
输出结果是
怎么理解,有权威的解读或者定义能够解释吗
<<深度探索C++对象模型>> 这本书有详细介绍,可以解答你的疑惑!!
感谢!,晚点去看看
说白了程序就是内存和CPU,这里实例化的clazz ,也有他的内存地址,所有的成员都会按顺序排列,还有虚表指针等