c++ - 函数返回值的引用

标签 c++

假设我有这样的东西

  class HandleInterface {
        GLuint handle_;
    protected:
        void SetHandle( GLuint i ) {
            handle_ = i;
        }

    public:
        GLuint GetHandle() const {
            return handle_;
        }

        virtual ~HandleInterface() {}
};

但是现在我需要 handle_ 的引用。

// I would need &handle_
glGenBuffers( 1,&handle_ );

我是否需要为我的 handle_ 编写另一个 getter?或者如何从我的 getter 方法中获取 handle_ 的引用?

此外 glGenBuffers 以某种方式违反了我的 SetHandle 方法,因为它应该只通过 SetHandle 方法设置 handle_ 而不是虽然是引用。有什么办法可以避免这种情况吗?

最佳答案

如果你想保持handle_完全封装,传递一个局部变量给glGenBuffers:

GLuint handle;
glGenBuffers(1, &handle);
SetHandle(handle);

关于c++ - 函数返回值的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16462006/

相关文章:

c++ - boost shared_ptr 传递派生类

c++ - 搜索\n 和\t

c++ - 从订购的容器中制作比较器

c++ - VS2008 中的 char[] 问题 - 为什么 strcat 附加到空数组的末尾?

c++ - 为类数据成员调用复制构造函数和 operator=

C++ 带指针的拷贝构造函数

c++ - Eclipse Luna GDB 调试器支持

c++ - 为什么 C++ 编译器有模糊的错误消息

c++ - 调用mono_jit_init时退出1

c++ - 在 Visual Studio 项目目录中找不到头文件