[英]c++ Constructor member initialization : pass arguments
In my class I have private member variable 's3_client' as shown in the following code snippet在我的班级中,我有私有成员变量“s3_client”,如下面的代码片段所示
class GpuMatcher
{
private:
Aws::SDKOptions options;
Aws::S3::S3Client s3_client;
..
public:
GpuMatcher();
..
};
To configure 's3_client' I need to create some other dependent objects as follows :要配置“s3_client”,我需要创建一些其他依赖对象,如下所示:
Aws::Client::ClientConfiguration config;
std::string region=AppContext::getProperty("region");
Aws::String aws_region(region.c_str(), region.size());
config.region=aws_region;
Aws::S3::S3Client s3_client(config); //initialize s3_client
My question is, how can I initialize this in the class constructor ?我的问题是,如何在类构造函数中初始化它?
GpuMatcher::GpuMatcher() : options() , s3_client(???)
{
}
If you need to pass arguments to this and also store the configuration (perhaps?):如果您需要将参数传递给它并存储配置(也许?):
class GpuMatcher
{
private:
Aws::SDKOptions options;
Aws::Client::ClientConfiguration config;
Aws::S3::S3Client s3_client;
static const Aws::Client::ClientConfiguration& populate_region(Aws::Client::ClientConfiguration& config);
..
public:
GpuMatcher();
..
};
and then:进而:
GpuMatcher::GpuMatcher() : options() , s3_client(populate_region(config))
{
}
Be aware of the order here, as the config must be created before the client.请注意此处的顺序,因为必须在客户端之前创建配置。
If you don't need to store the config (if it's a pass-by-value for the constructor), no need to pass in config
to populate_region
(and then it's a create_config
).如果您不需要存储配置(如果它是构造函数的按值传递),则无需将
config
传递给populate_region
(然后它是一个create_config
)。
Make a function to generate the config
object.创建一个函数来生成
config
对象。 eg例如
class GpuMatcher
{
...
private:
static Aws::Client::ClientConfiguration generateConfig() {
Aws::Client::ClientConfiguration config;
std::string region=AppContext::getProperty("region");
Aws::String aws_region(region.c_str(), region.size());
config.region=aws_region;
return config;
}
};
then然后
GpuMatcher::GpuMatcher() : options() , s3_client(generateConfig())
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.