c++ - transient 复制构造函数怪异

标签 c++ stl

我在头文件中定义了这个类:

class E_IndexList {
public:
    E_UIntegerList* l;
    inline void *data() { // retrieve packed data: stride depends on type (range)
        return l->data();
    }
    inline void insert(unsigned value) {
        if (value > maxval[l->range]) {
            promote();
            insert(value);
        } else {
            l->push_back(value);
        }
    }
    inline size_t size() {
        return l->size();
    }
    inline unsigned long get(int index) {
        return l->get(index);
    }
    void promote() {
        if (l->range == E_UIntegerList::e_byte) {
            E_UShortList *new_short_list = new E_UShortList(*((E_UByteList*)l));
            delete l;
            l = new_short_list;
        } else if (l->range == E_UIntegerList::e_short) {
            E_UIntList *new_int_list = new E_UIntList(*((E_UShortList*)l));
            delete l;
            l = new_int_list;
        } else ASSERT(false);
    }
    // start off with bytes by default
    E_IndexList() {
        l = new E_UByteList;
    }
    E_IndexList(E_UIntegerList::int_bits range) {
        switch(range) {
        case E_UIntegerList::e_byte:
            l = new E_UByteList;
            break;
        case E_UIntegerList::e_short:
            l = new E_UShortList;
            break;
        case E_UIntegerList::e_int:
            l = new E_UIntList;
            break;
        default:
            ASSERT(false);
            break;
        }
    }
    E_IndexList(const E_IndexList& cpy) { // copy ctor
        switch(cpy.l->range) {
        case E_UIntegerList::e_byte:
            l = new E_UByteList(((E_UByteList*)cpy.l)->list);
            break;
        case E_UIntegerList::e_short:
            l = new E_UShortList(((E_UShortList*)cpy.l)->list);
            break;
        case E_UIntegerList::e_int:
            l = new E_UIntList(((E_UShortList*)cpy.l)->list);
            break;
        default:
            ASSERT(false);
            break;
        }
    }
    ~E_IndexList() {
        delete l;
    }
};

这里还有一些它使用的类:

static const unsigned long maxval[] = {0xff,0xffff,0xffffffff};
class E_UIntegerList {
public:
    enum int_bits {e_byte = 0, e_short = 1, e_int = 2};
    virtual ~E_UIntegerList() {}
    int_bits range;
    virtual void push_back(int i) = 0;
    virtual void *data() = 0;
    virtual size_t size() = 0;
    virtual unsigned long get(int index) = 0;
};
struct E_UByteList:public E_UIntegerList {
    std::vector<unsigned char> list;
    E_UByteList() {
        range = e_byte;
    }
    E_UByteList(const std::vector<unsigned char>& copy) {
        list = copy;
    }
    inline void push_back(int i) {
        list.push_back(i);
    }
    inline void *data() { return list.data(); }
    inline size_t size() { return list.size(); }
    inline unsigned long get(int index) { return list[index]; }
};
struct E_UShortList:public E_UIntegerList {
    std::vector<unsigned short> list;
    E_UShortList() {
        range = e_short;
    }
    E_UShortList(const std::vector<unsigned short>& copy) {
        list = copy;
    }
    E_UShortList(const E_UByteList& promotee) {
        range = e_short;
        list.assign(promotee.list.begin(),promotee.list.end()); // assignment should be compatible
    }
    inline void push_back(int i) {
        list.push_back(i);
    }
    inline void *data() { return list.data(); }
    inline size_t size() { return list.size(); }
    inline unsigned long get(int index) { return list[index]; }
};
struct E_UIntList:public E_UIntegerList {
    std::vector<unsigned int> list;
    E_UIntList() {
        range = e_int;
    }
    E_UIntList(const std::vector<unsigned int>& copy) {
        list = copy;
    }
    E_UIntList(const E_UShortList& promotee) {
        range = e_int;
        list.assign(promotee.list.begin(),promotee.list.end());
    }
    inline void push_back(int i) {
        list.push_back(i);
    }
    inline void *data() { return list.data(); }
    inline size_t size() { return list.size(); }
    inline unsigned long get(int index) { return list[index]; }
};

现在我使用这个类的方式是我有一个 std::vector<E_IndexList>我用作索引列表的容器。

奇怪的是,当我运行程序时,有时它没有问题,有时它断言为 false。

所以这对我来说是一个大危险信号,因为发生了一些非常可疑的事情。我很可能最终会放弃整个 E_IndexList直到我开始研究还有很长一段路要走的游戏网络代码。但是,我想知道这里发生了什么。

我拥有的每个 ctor 都将范围设置为 E_UIntegerList 中枚举之外的有效值,那么这个断言怎么会被绊倒呢?而且我无法开始想出为什么行为不一致的解释。调用这段代码的测试不是多线程的。

最佳答案

您的 E_UByteList 来自 vector 的构造函数未设置 range 值。

整个设计有点粗制滥造;您应该学习如何使用构造函数初始化列表,我可能会为基类赋予一个 protected 构造函数,该构造函数设置 range 值并且可以从派生构造函数的初始值设定项中调用。

关于c++ - transient 复制构造函数怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252505/

相关文章:

c++ - std::priority_queue 带有指向 std::map 元素的迭代器 - 无效堆

c++ - 练习: split the array values into positive and negative arrays

c++ - 重载 operator->() 以更改被调用函数的返回值

c++ - 为什么我们不能声明一个 std::vector<Abstract Class>?

创建内存分配器时出现 C++ 继承错误

c++ - STL 对和二进制搜索

c++ - 磁盘支持的 STL 容器类?

c++ - 是否有任何用于 Bloomier 过滤器的标准库在运行?

c++ - 如何正确定义和调用原始运算符<<

c++ - std::tuple_size 和引用