通过将初始化器列表传递给生成器函数来初始化某种类型的对象是可能的 [英] would it be possible to initialize an object of a certain kind by passing an initializer list to a generator function

查看:62
本文介绍了通过将初始化器列表传递给生成器函数来初始化某种类型的对象是可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有可生成某种类型对象的函数的工厂类.但是,目前,此函数在实例化对象并将其返回到程序的主作用域之前,先使用一组明确的参数.这些对象是使用随机数作为其参数生成的.在某个时候,我将重载我创建的对象的类的构造函数以接受更多或更少的参数.因此,为避免在我的工厂类中创建许多函数来生成具有不同参数集的同一对象,我认为我可以使用初始化列表.我尝试实现此解决方案,但失败了.有人可以指教我,我做错了什么吗?或者也许将我指向正确的方向:)

I have a factory class with a function to generate a certain type of objects. However, at present moment, this function takes an explicit set of parameters before instantiating an object and returning it back in the main scope of the program. These objects are generated using random numbers as its parameters. At some point, I will overload the constructor of the class of which objects I create to take more or less parameters. Therefore to avoid creating many functions in my factory class generating the same object with varying set of parameters, I thought I could use an initialization list. I have tried to implement this solution but I failed miserably. Could someone please enlighten me what do I do wrong? Or perhaps point me in the right direction :)

以下是我正在尝试做的一个基本示例

Below is a basic example of what I am trying to do

#include <iostream>
#include <initializer_list>

// iostream
using std::cout;
using std::endl;

// initializer list
using std::initializer_list;

// Class of which type I want to generate objects;
class Subject{
private:
  double x;
  double y;
public:
  Subject(const double x, const double y); // <-- This may vary in parameter set!
  void print();
};

// Class which is responsible for generating objects of type specified above;
class Generator{
public:
  Generator();
  Subject generate(initializer_list<double> params);
};

// Subject Member Implementation
Subject::Subject(const double x, const double y):x(x), y(y){}

void Subject::print(){
  std::cout << "x: " << x << "y: " << y << std::endl;
}

// Generator Member Implementation
Generator::Generator(){}

Subject Generator::generate(initializer_list<double> params){
  Subject subject = params;

  return subject;
}

// Main Body
int main(){
  Generator generator;
  double params[2] = {.1,.2}; // <-- arbitrary set of parameter(s)

  Subject subject = generator.generate({params}); // <-- object cration;

  subject.print();
}

推荐答案

可以使用,但是必须考虑类型和隐式类型.

It could be made to work, but the types and implicit types must be considered.

以下是一些可行与不可行的示例:

Here are some examples of what would work vs. what would not work:

#include <initializer_list>
#include <iterator> 

void f(std::initializer_list<int> il) {}

int main()
{
    int parameters[4]{1, 2, 3, 4};
    auto test1 = {parameters};  // parameters decays into an int*, so we get an initializer_list of int*s (single pointer in this case)

    // named parameter causes problems
    //f(parameters);    // doesn't compile, can't convert array to initializer_list
    //f({parameters});  // doesn't compile, initializer_list types are different
    //f({std::begin(parameters), std::end(parameters)}); // doesn't compile, considered as list of pointers

    // this works on MSVC, but is probably not standard C++
    std::initializer_list<int> test2(std::begin(parameters), std::end(parameters)); // use constructor that accepts iterators
    f(std::initializer_list<int>(std::begin(parameters), std::end(parameters))); // ok
    f(test2);           // ok

    f({1, 2, 3, 4});    // ok, can construct parameter with this initializer
    f({});              // ok, for the same reason as above
}

这篇关于通过将初始化器列表传递给生成器函数来初始化某种类型的对象是可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆