简体   繁体   English

C++ class 由成员初始化器列表构造,带有指向成员地址的指针

[英]C++ class construction by member initializer list with pointer to member address

I've got a C based library (Vulkan to be precise) that is initializing and setting values by passing their address to a library function as parameter.我有一个基于 C 的库(准确地说是 Vulkan),它通过将它们的地址作为参数传递给库 function 来初始化和设置值。 To prevent any leaks and to make sure that the clean up is called (even when some exception is happening somewhere) I want to encapsulate that value in a RAII class that calls the create and destroy library functions for it:为了防止任何泄漏并确保调用清理(即使某处发生异常),我想将该值封装在调用createdestroy库函数的 RAII class 中:

value_t value; // value must live somewhere else
library_create_value( value_t* value );
library_destroy_value( value_t value ); // must not be forgotten to call at the end

A C program would have no issues to work correctly here: C 程序在这里正常工作没有问题:

int main() {
  value_t myValue;                // fine, myValue lives on the stack of main()
  library_create_value( &value ); // and is ready to be used now

  // do all work, use myValue

  library_destroy_value( value ); // the required clean up
  return 0;
}

But now writing the C++ RAII wrapper I struggle to find the best solution to initialize the value as I'd normally do:但是现在编写 C++ RAII 包装器,我很难像往常一样找到初始化value的最佳解决方案:

class RAII_wrapper {
  value_t myValue;

public:
  RAII_wrapper() : 
    myValue( library_create_value() ) // doesn't work here as it would normally do!
  {} 

  ~RAII_wrapper() {
    library_destroy_value( value );   // nothing special here
  }

  // other methods to use myValue would be here
}

Of course I could just do the creation call in the constructor itself and leave myValue uninitialized till then - but that is (called somewhere) "no good style".当然,我可以只在构造函数本身中进行创建调用,然后让myValue未初始化 - 但那是(在某处调用)“没有好的风格”。

What's the (officially) best solution for this task?这项任务的(官方)最佳解决方案是什么?

It sounds like you desire to wrap a pointer for an object on the heap even though that is not the usage in main() that you provided.听起来您希望在堆上包装指向 object 的指针,即使这不是您提供的 main() 中的用法。 A small change will allow this while also ensuring you only create the object once.稍作改动即可实现这一点,同时确保您只创建一次 object。 The first example looks like you may construct the object twice, once with default constructor and again to modify it.第一个示例看起来您可以构造 object 两次,一次使用默认构造函数,然后再次修改它。 See example below:请参见下面的示例:

class RAII_wrapper{
    private:
        value_t *myValue; // This maintains same function call as example
    public:
        RAII_wrapper(){
            library_create_value(myValue); 
        }
        ~RAII_wrapper(){
            library_destroy_value(myValue); 
        }
};

Edit: If this wasn't a C library you could utilize smart pointers instead of the raw pointer to at least avoid the library_destroy_value() call.编辑:如果这不是 C 库,您可以使用智能指针而不是原始指针来至少避免 library_destroy_value() 调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM