const成员和operator = [英] const members and operator=

查看:163
本文介绍了const成员和operator =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 struct 和一些 const 变量

struct HashData
{
    const HashKey key;
    const void* data;
    HashData(const HashKey& key_, const void* data_) : key(key_), data(data_) {}
    /* How to write this ?
    HashData operator=(const HashData & data)
    {
        key = std::move(data.key);
        return *this;
    }
    */
};

和另一个使用它的类。

class HashTable
{
    std::vector< std::list<HashData> > hashTable; ///< Collisions resolution by chaining.
    public:
    void insertAtIndex(const std::size_t index, const HashData& data) {
        hashTable[index].insert(std::begin(hashTable[index]), data);
    }
};

class HashTable p>

class HashTable compiles but another class

class OpenAddressHashTable 
{
    std::vector<HashData> hashTable;
    public:    
    void insert(const HashData & data) throw() {
        if (data.key == NULLKEY)
            throw std::runtime_error("Do not use NullKey");

        size_t iteration = 0;
        do {
            const size_t index = (*hashFunc)(data.key, iteration);
            if (hashTable[index].key == NULLKEY) {
                // space is free
     // ** IMPORTANT **///// Line 131 is next line 
                hashTable[index] = data;
                return ;
            }
            iteration++;
        } while(iteration < hashTable.size());

        throw std::runtime_error("No space left");
    }
 };

我收到此错误:

g++ -W -Wall -pedantic -std=c++11 hash.cpp 
hash.cpp: In member function 'void OpenAddressHashTable::insert(const HashData&)':
hash.cpp:131:24: error: use of deleted function 'HashData& HashData::operator=(const HashData&)'
hash.cpp:26:8: note: 'HashData& HashData::operator=(const HashData&)' is implicitly deleted because the default definition would be ill-formed:
hash.cpp:26:8: error: non-static const member 'const HashKey HashData::key', can't use default assignment operator

std: :list 是否需要将数据放入我的向量?
我需要在 hashTable

What is it that std::list does that i need to put the data in my vector ? Do i need to use pointers in my hashTable ?

推荐答案

p>您正在进行作业:

You are doing an assignment:

hashTable[index] = data;

如果你有 const 成员,因为您不能复制或移动到 const 。编译器错误很明显:

There is simply no way for that to work if you have const members because you cannot copy or move into const. The compiler error is pretty explicit:


[赋值运算符]被隐式删除,因为默认定义将是错误的

[the assignment operator] is implicitly deleted because the default definition would be ill-formed

您期望分配到 ?最简单的办法是删除 const 并在用户的界面上强制使用 - 以便他们不能从下面更改键。例如:

What would you expect the assignment to do to key and data? The simplest thing would be to drop the const and enforce it on your interface with the user - so that they cannot change the key out from under you. For instance:

using InternalHashData = std::pair<HashKey, void*>; 
using ExternalHashData = std::pair<const HashKey, void*>;

InternalHashData& something_to_be_returned = ..; // never expose this
return reinterpret_cast<ExternalHashData&>(something_to_be_returned); // this is OK

我可以想到的唯一方法是 const 将更改您的表格:

The only way I can think of to keep the const would be to change your table from:

std::vector<HashData> hashTable;

std::vector<std::unique_ptr<HashData>> hashTable;

但是,你在每个插入上做一个额外的分配只是为了保留 const -ness,这似乎不是一个很好的权衡,特别是对于一个容器,其唯一的目的是性能。

But then you're doing an extra allocation on each insert just to preserve const-ness, which doesn't seem like a good tradeoff to me at all, especially for a container whose sole purpose is performance.

这篇关于const成员和operator =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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