Skip to main content

Command Palette

Search for a command to run...

C/c++隐藏结构体/类成员的方法

Updated
1 min read

纯C实现隐藏结构体(实现不透明结构体)的方法:

假设我们要开发一个库example.so,但又不想把某结构体的内容开放到SDK的头文件中,那么可以这样处理:

  1. 准备三个文件:example.h(SDK的头文件),example_internal.h(不开放,存放了结构体的真实定义),example.c

  2. 各文件内容如下:

    example.h:

     /* opaque types */
     struct example_struct;
     /* helper functions */
     __declspec( dllexport ) struct example_struct* create_example(const int id,const float rate);
     __declspec( dllexport ) const int get_example_id(struct example_struct *example);
     __declspec( dllexport ) const float get_example_rate(struct example_struct *example);
     __declspec( dllexport ) void free_example(struct example_struct *example);
    

    example_internal.h:

     /* The real structure */
     struct example_struct {
     int id;
     float rate;
     int hiddenInt;
     float hiddenRate;
     };
    

    example.c:

     #include "example.h"
     #include "example_internal.h"
     struct example_struct* create_example(const int id,const float rate){
         struct example_struct *example=(struct example_struct*)malloc(sizeof(struct example_struct));
         return example;
     }
    
     const int get_example_id(struct example_struct *example){
         if(!example){
             return 0;
         }
         return example->id;
     }
    
     /* Ohter helper functions ... */
    
  3. 生成好库后,只需要把example.so和example.h打包进SDK里就可以被用来开发了

C++实现隐藏类成员的方法:

还是以example.so为例:

  1. 在头文件中定义一个不透明结构体/类,实际的内容放在实现文件中,注意:由于编译时,不能明确知道它的构造及析构函孙,因此无法使用智能指针

    example.hpp

     __declspec( dllexport ) class example {
     public:
     const int getId() const;
     const float getRate() const;
     private:
     class exampleImpl;
     //opaque pointer;
     exampleImpl *m_impl;
     };
    

    example.cpp

  2.    #include "example.hpp"
       class exampleImpl {
       public:
        int m_id;
        float m_rate;
        int m_hideId;
        float m_hideRate;
       };
    
       const int example::getId() const{
           return this->m_impl->m_id;
       }
       const float getRate() const{
           return this->m_impl->m_rate;
       }
    

More from this blog

解决QWidget用winId获取HWND而导致Qt程序无事件消息的问题

问题原因: 当对一个QWidget调用winId时,默认情况下,Qt会对该窗口进行Native化,从而导致如鼠标等事件被其它原生窗口接管,表现出来的现像就是窗口不响应任何事件 解决思路: 防止Qt窗口Native化 解决办法: 第一步,在创建QApplication对象之前设置: QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); //注意:Qt::AA_NativeWindows受环境变量 QT_USE_NATI...

Jul 7, 20251 min read

解决因OpenGL渲染窗口高宽比导致图形变形

在 OpenGL 中,由于窗口的 宽高比(aspect ratio) 与绘制内容的坐标系统不一致,图像会出现拉伸、压缩等变形现象。为了解决这个问题,可根据窗口的大小调整投影矩阵,确保图像在视觉上保持原始比例。 先通过glViewPort调整视口 void resizeGL(int w, int h) { glViewport(0, 0, w, h); } 再根据窗口高宽比,计算出合适的投影矩阵,这样体现出来的样式,就是截掉了宽高比之外的内容,显示的内容不变形,正圆就是正圆,不会被拉伸成...

Jun 18, 20251 min read

cmake管理使用了qt的项目的正确使用方法

可将QT5_DIR(包括了bin/inclue/lib等目录的那个基础目录,不是lib/cmake)加到CMAKE_PREFIX_PATH中,防止find_package无法使用 cmake对qt moc有如下几个函数封装: qt_wrap_ui([输出]MOC后的源文件列表 [输入]MOC前的.ui文件) qt_wrap_cpp([输出]MOC后的源文件列表 [输入]MOC前的源文件,通常是包含了Q_OBJECT的.hpp) qt_add_resources([输出]MOC后的源文件列表 [输...

Apr 7, 20251 min read

源赖朝的部落格威力缩小版

28 posts

源赖朝的部落格威力缩小版