C++ 前向声明

看OCC代码时候看到,class V3d_Viewer;

这是一个C++的前向声明,在 C++ 中,class V3d_Viewer; 是一个前向声明(forward declaration),用于告诉编译器 V3d_Viewer 是一个类,但不提供其具体定义。通过前向声明,可以在不需要知道 V3d_Viewer 类的具体实现的情况下引用它,从而减少编译依赖并提高编译效率。

这种声明通常用于以下情况:

  1. 减少编译依赖:如果你只需要在头文件中引用指向该类的指针或引用,而不需要知道该类的具体实现,就可以使用前向声明来避免包含不必要的头文件,从而减少编译时间和编译依赖。

  2. 解决依赖循环:如果两个类相互引用对方,使用前向声明可以打破这种循环依赖,避免编译错误。

假设你有一个类 V3d_Viewer,它的具体定义在 V3d_Viewer.hxx 头文件中。你可能在另一个类的头文件中使用前向声明来引用它,例如:

复制代码
// ViewerManager.hxx
#ifndef VIEWERMANAGER_HXX
#define VIEWERMANAGER_HXX

class V3d_Viewer;  // 前向声明

class ViewerManager {
public:
    ViewerManager();
    ~ViewerManager();

    void SetViewer(V3d_Viewer* viewer);
    V3d_Viewer* GetViewer() const;

private:
    V3d_Viewer* myViewer;
};

#endif // VIEWERMANAGER_HXX

ViewerManager 类使用前向声明 class V3d_Viewer; 来声明 myViewer 成员变量是一个指向 V3d_Viewer 类的指针,并在成员函数中引用它。具体的 V3d_Viewer 类定义在cpp文件中包含

复制代码
// ViewerManager.cxx
#include "ViewerManager.hxx"
#include "V3d_Viewer.hxx"  // 包含 V3d_Viewer 类的具体定义

ViewerManager::ViewerManager() : myViewer(nullptr) {}

ViewerManager::~ViewerManager() {}

void ViewerManager::SetViewer(V3d_Viewer* viewer) {
    myViewer = viewer;
}

V3d_Viewer* ViewerManager::GetViewer() const {
    return myViewer;
}
相关推荐
淼淼76314 小时前
安装jdk1.8
java·开发语言
PfCoder14 小时前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
Legendary_00815 小时前
Type-C 一拖二快充线:突破单口限制的技术逻辑
c语言·开发语言
过期动态15 小时前
Java开发中的@EnableWebMvc注解和WebMvcConfigurer接口
java·开发语言·spring boot·spring·tomcat·maven·idea
智者知已应修善业15 小时前
【查找字符最大下标以*符号分割以**结束】2024-12-24
c语言·c++·经验分享·笔记·算法
csbysj202015 小时前
Web 标准
开发语言
91刘仁德15 小时前
c++类和对象(下)
c语言·jvm·c++·经验分享·笔记·算法
老姚---老姚15 小时前
在windows下编译go语言编写的dll库
开发语言·windows·golang
diediedei15 小时前
模板编译期类型检查
开发语言·c++·算法
穿过锁扣的风16 小时前
零基础入门 Python 爬虫:从基础到实战,爬取虎扑 / 豆瓣 / 图片全掌握
开发语言·爬虫·python