#include <iostream>
#include<string>
class Entity {
public:
void print()const { std::cout << "helo!" << std::endl; }
};
class ScopedPtr {
private:
Entity* m_obj;
public:
ScopedPtr(Entity* entity)
:m_obj(entity)
{
}
~ScopedPtr()
{
delete m_obj;
}
Entity* GetObject()
{
return m_obj;
}
Entity* operator->()
{
return m_obj;
}
const Entity* operator->() const
{
return m_obj;
}
};
struct vector3 {
float x, y, z;
};
int main()
{
ScopedPtr entity = new Entity();
const ScopedPtr entity1 = new Entity();
const Entity* entity2 = new Entity();
entity.GetObject()->print();
entity->print();
entity1->print();
entity2->print();
int offset_x = (int)&((vector3*)nullptr)->x;
int offset_y= (int)&((vector3*)nullptr)->y;
std::cout <<"offset_x=" << offset_x << ",offset_y=" << offset_y << std::endl;
}
c++的->操作符,运算符重载,及获取变量的地址偏移
于 2023-03-14 21:38:17 首次发布