c++ - 如何让类在没有 getter 的情况下返回其他类型?

标签 c++ class

我的代码:

class MyWindowSettings {

public:

    const ofGLFWWindowSettings& get(){
        return settings;
    }

private:
    ofGLFWWindowSettings settings;
};

如果没有 getter,我如何将此类用于需要 ofGLFWWindowSettings 而不是 MyWindowSettings 的函数?

我现在是

MyWindowSettings settings;
ofCreateWindow(settings.get());

但我想像下面那样做

MyWindowSettings settings;
ofCreateWindow(settings);

我该怎么做?

最佳答案

您可能要做的是使用 user-defined convertion运算符(operator)

class MyWindowSettings {

public:

    operator const ofGLFWWindowSettings&(){
        return settings;
    }

private:
    ofGLFWWindowSettings settings;
};

关于c++ - 如何让类在没有 getter 的情况下返回其他类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51151395/

相关文章:

c++ - Objective C 类方法 == C++ 构造函数?

c++ - 错误:成员- “Graph::V”不是类型名称

java - java类中的类?

c++ - 在类中操作 vector 时遇到问题

c++ - "I' m busy please wait"窗口 - 没有按钮

c++ - 导出为 PDF 时出错(RPC 服务器不可用)

c++ - 有符号整数溢出为负数 : is this a compiler bug, 还是我误解了优化?

java - 如何正确地从类中返回一个字符串?

c++ - 使用两个不同版本的boost

c++ - #if elif 指令未按预期工作