栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【46】C++中的箭头操作符

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【46】C++中的箭头操作符

示例:箭头操作符的使用及其重载:

#include 
#include 

class Entity
{
public:
	int x;
public:
    void Print() const {std::cout << "Hello" << std::endl;}
};

class ScopedPtr
{
private:
    Entity* m_Obj;
public:
    ScopedPtr(Entity* entity)
        : m_Obj(entity)
        {   
        }
    ~ScopedPtr()
    {
        delete m_Obj;
    }
    
    const Entity* operator->() const
    {
        return m_Obj;
    }
};

int main()
{
    Entity e;
    e.x = 1;
    e.Print();
    
    Entity* ptr = &e;
    ptr->x = 2;
    ptr->Print();  //等价于:(*ptr).Print();
    
    const ScopedPtr* entity = new Entity();//const指针只能调用const型的成员函数
    entity->Print();
    
    return 0;
}

我们主要分析以下第41、42行代码,当我们将其替换成Entity* entity = new Entity(); entity->Print();时,编译器并不会报错,因为ScopedPtr中并没有m_Obj,我们定义的是Scopedptr型的entity,编译器无法找到Entity的成员函数Print,所以我们需要重载->。

示例:箭头操作符的应用(获取内存中某个成员变量的偏移量)

#include 
#include 

struct Vector3
{
    float x, y, z; //float是四字节
};

int main()
{
    int offset1 = (int)&((Vector3*)nullptr)->x;
    std::cout << offset1 << std::endl; // 0
    
    int offset2 = (int)&((Vector3*)nullptr)->y;
    std::cout << offse2t << std::endl; // 4
    
    int offset3 = (int)&((Vector3*)nullptr)->z;
    std::cout << offset3 << std::endl; // 8
}

注意第11、14、17行代码逻辑,定义一个Vector3类型的空指针,使用其分别访问成员变量x,y,z,然后取其地址并强制转化为int型,最后进行赋值。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780434.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号