Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Arrow operator in the constructor of a class C++

I am working in a Hash Table in C++ and I wanted it to be dynamically allocated to support a case where the table might be declared with a very large size.

The code is working and it looks like:

class HashTable {
private:
    static const int m_TableSize = 4;

    struct item {
        std::string name;
        std::string drink;
        item* next;
    };

    item* m_Table;

public:
    HashTable();
    ~HashTable();
};

HashTable::HashTable()
{
    m_Table = new item[m_TableSize];
    for (int i = 0; i < m_TableSize; i++)
    {
        m_Table[i].name = "empty";
        m_Table[i].drink = "empty";
        m_Table[i].next = nullptr;
    }
}

HashTable::~HashTable() {
    delete[] m_Table;
}

Here’s my question:
Even though I am using the new keyword and the table is defined as an array of pointers item* wouldn’t it be necessary, in the constructor, to use the arrow operator to access the member variables inside m_Table as in the line below?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

m_Table[i]->name = "empty";

Initially, I tried to use the arrow operator, but I got a compiler error: "type ‘HashTable::item’ does not have an overloaded ‘member operator ->’ ".

>Solution :

Your confusion is understandable. The m_Table variable is indeed a pointer, but it points to an array of item structs, not an array of pointers to item structs. So when you use m_Table[i], you are accessing an item struct directly, not a pointer to an item struct.

When you access an item struct directly, you should use the dot operator to access its member variables, like so:

m_Table[i].name = "empty";
m_Table[i].drink = "empty";
m_Table[i].next = nullptr;

If you had an array of pointers to item structs instead, you would use the arrow operator like you mentioned:

m_Table[i]->name = "empty";

To create an array of pointers to item structs, you would change the type of m_Table and the memory allocation in the constructor accordingly:

item** m_Table;
HashTable::HashTable()
{
    m_Table = new item*[m_TableSize];
    for (int i = 0; i < m_TableSize; i++)
    {
        m_Table[i] = new item;
        m_Table[i]->name = "empty";
        m_Table[i]->drink = "empty";
        m_Table[i]->next = nullptr;
    }
}

But since your current implementation is already working correctly and you’re using an array of item structs directly, you should keep using the dot operator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading