从外部访问类中的私有成员

#include <iostream>


template<typename Tag, typename Tag::type M>
struct Rob {
    friend typename Tag::type get(Tag){
        return M;
    }
};

// use
struct A {
    A(int a):a(a){}
private:
    int a;
};

// tag used to access A::a
struct A_f{
    typedef int A::*type;
    friend type get(A_f);
};

template struct Rob<A_f, &A::a>;

int main(){
    A a(42);
    std::cout << "proof:" << a.*get(A_f()) << std::endl;
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 04:02:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 04:02:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 04:02:02       82 阅读
  4. Python语言-面向对象

    2024-06-08 04:02:02       91 阅读

热门阅读

  1. kafka连接zookeeper失败导致无法启动

    2024-06-08 04:02:02       29 阅读
  2. 机器学习 - 常见问题与解决方案

    2024-06-08 04:02:02       29 阅读
  3. 机器学习目录

    2024-06-08 04:02:02       31 阅读
  4. 网络安全现状

    2024-06-08 04:02:02       31 阅读
  5. 人脸识别系统代码--预测年龄

    2024-06-08 04:02:02       32 阅读
  6. C# 语言类型(三)—数组/枚举类型/结构体

    2024-06-08 04:02:02       33 阅读
  7. AI初识--LLM、ollama、llama都是些个啥?

    2024-06-08 04:02:02       35 阅读
  8. 富格林:总结阻挠欺诈解决措施

    2024-06-08 04:02:02       31 阅读