Maintain Object State Information when Calling Overloaded Constructor from Default Constructor


That question title is a mouthful. Basically, I am creating a hash table structure that uses doubly linked lists within a vector. Everything works fine when I create the object using my overloaded constructor, but using the default constructor causes the state of the object to go funky after returning to main.

My constructors:


    HashTable::HashTable(int tableSize)
            currentSize = tableSize;

Setting breakpoints after creating an object

    HashTable ht(size); //this works
    HashTable ht;       //this does not work

Stepping through the code I see that it calls the overloaded constructor fine, but after returning to main and then attempting to use the table (when using default constructor only), the vector's size and the variable currentSize have gone awry.

After creating object, before returning to main:

currentSize = 53
table [size] = 53, [capacity] = 53, empty linked lists fill the vector

When calling ht.hash(value) in main, the object now has:

currentSize = -858993460
table [size] = 0, [capacity] = 0, linked lists obviously gone.

What would cause the vector to reset itself to 0 and my private int currentSize to go funky, especially since the code paths both work through HashTable(int tableSize)?


@dyp Pointed me in the right direction.

HashTable(53); was creating a temporary local object - not setting the object in main to my desired size of 53.

To call the overloaded constructor on my object in main rather than create a temporary object, this->HashTable::HashTable(53); worked (in Visual Studio) to force the overloaded constructor to be called on my calling object.

EDIT: gcc compiler forbids this, and this is generally considered bad practice, whether your compiler allows it or not.

An initializer list as HashTable::HashTable() : HashTable(53) {} is considered to be the correct way to accomplish what I was trying to do.