具有const数组和initializer_list的C ++重载构造函数 [英] C++ overload constructor with const array and initializer_list

查看:101
本文介绍了具有const数组和initializer_list的C ++重载构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,该类在构造函数中将引脚的列表作为参数。
现在,此列表只是一个const数组( const unsigned int(&spins)[N] )。
我想以可以使用initializer_list和(const)数组的方式重载构造函数。

我可以使它与单独的类一起工作,但是我m无法将它们组合在一起。

I have a class that takes a "list" of pins as argument in the constructor. Right now, this "list" is just a const array (const unsigned int (&pins)[N]). I'd like to overload the constructor in such a way that you can use an initializer_list as well as a (const) array.
I can get it working with separate classes, but I'm unable to combine them.

我的测试班的基本结构:

The basic structure of my test class:

#include <iostream>
#include <cstring>
using namespace std;

class X
{
  public:
    X(... pins);
    ~X();
    void print() {
        for (unsigned int i = 0; i < length; i++)
            cout << pins[i] << ' ';
    }

  private:
    (const) unsigned int *pins;
    const size_t length;
};

A类(使用std :: initializer_list)

Class A (using std::initializer_list)

  public:   
    A(initializer_list<unsigned int> pins)
        : length(pins.size()) {
        this->pins = (unsigned int *)malloc(sizeof(unsigned int) * pins.size());
        memcpy(this->pins, pins.begin(), sizeof(unsigned int) * pins.size());
    }
    ~A() {
        free(pins);
    }
  private:
    unsigned int *pins;

B类(使用数组)

  public:
    template <size_t N>
    B(unsigned int (&pins)[N])
        : length(N) {
        this->pins = pins;
    }
  private:
    unsigned int *pins;

C类(使用const数组)

Class C (using a const array)

  public:
    template <size_t N>
    C(const unsigned int (&pins)[N])
        : length(N) {
        this->pins = pins;
    }
  private:
    const unsigned int *pins;

这允许我执行以下操作:

This allows me to do things like:

A a({1, 2, 3});

unsigned int pb[] = {4, 5, 6};
B b(pb);

const unsigned int pc[] = {7, 8, 9};
C c(pc);

a.print();
b.print();
c.print();

这会打印 1 2 3 4 5 6 7 8 9 如预期。

如何将这些不同的构造函数组合到一个类中?
我希望能够以上面显示的三种方式之一初始化对象。

How can I combine these different constructors in one class? I want to be able to initialize an object in either of the three ways shown above.

如果我做成 pins 一个 const ,我不能做 memcpy(pins,...)之类的事情,并且如果我不将其设为 const ,则不能使用const数组作为构造函数的参数。

If I make pins a const, I can't do things like memcpy(pins, ...), and if I don't make it a const, I can't use a const array as argument for the constructor.

(目标平台是Arduino,因此不支持大多数花哨且占用大量内存的C ++ 11技巧。)

(The target platform is Arduino, so most fancy and memory-intensive C++11 tricks are not supported.)

推荐答案

遵循以下思路:

class X
{
  public:
    X(std::initializer_list<unsigned int> aPins)
        : storage(aPins), length(aPins.size()) {
      pins = length ? &storage[0] : nullptr;
    }

    template <size_t N>
    X(unsigned int (&aPins)[N])
        : length(N) {
        pins = aPins;
    }

    template <size_t N>
    X(const unsigned int (&aPins)[N])
        : length(N) {
        pins = aPins;
    }

    void print() {
        for (unsigned int i = 0; i < length; i++)
            std::cout << pins[i] << ' ';
    }

  private:
    const unsigned int* pins;
    const size_t length;
    std::vector<unsigned int> storage;
};

演示

这篇关于具有const数组和initializer_list的C ++重载构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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