在我的课堂上,我有私有成员变量“ s3_client”,如以下代码片段所示
class GpuMatcher
{
private:
Aws::SDKOptions options;
Aws::S3::S3Client s3_client;
..
public:
GpuMatcher();
..
};
要配置“ 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
我的问题是,如何在类构造函数中初始化它?
GpuMatcher::GpuMatcher() : options() , s3_client(???)
{
}
答案 0 :(得分:1)
如果您需要为此传递参数并存储配置(也许?):
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();
..
};
然后:
GpuMatcher::GpuMatcher() : options() , s3_client(populate_region(config))
{
}
请注意此处的顺序,因为必须在客户端之前创建配置。
如果您不需要存储配置(如果它是构造函数的传递值),则无需将config
传递到populate_region
(然后是{{ 1}})。
答案 1 :(得分:1)
使函数生成config
对象。例如
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;
}
};
然后
GpuMatcher::GpuMatcher() : options() , s3_client(generateConfig())
{
}