从默认构造函数调用重载构造函数时维护对象状态信息 [英] Maintain Object State Information when Calling Overloaded Constructor from Default Constructor

查看:250
本文介绍了从默认构造函数调用重载构造函数时维护对象状态信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那个问题标题是一口气。
基本上,我创建一个哈希表结构,使用向量中的双向链表。一切工作正常,当我使用我重载的构造函数创建对象,但使用默认的构造函数导致对象的状态返回到主要的时髦。



我的构造函数:

  HashTable :: HashTable()
{
HashTable
}

HashTable :: HashTable(int tableSize)
{
currentSize = tableSize;
table.resize(tableSize);
}

在创建对象后设置断点

  HashTable ht(size); // this works 
HashTable ht; //这不起作用

遍历代码我看到它调用重载的构造函数很好,但是在返回到main,然后尝试使用表(当使用默认构造函数时),向量的大小和变量currentSize已经失效。



创建对象之后返回主:

  currentSize = 53 
table [size] = 53,[capacity] = 53,列表填充向量

调用 ht.hash(value) in main,该对象现在有:

  currentSize = -858993460 
table [size] = 0 ,[capacity] = 0,链表明显走了。

什么会导致向量重置自己为0和我的私人int currentSize去时髦,代码路径都通过 HashTable(int tableSize)

解决方案

@dyp指向正确的方向。



HashTable(53); 正在创建一个临时本地对象



在main中调用我的对象的重载构造函数而不是创建一个临时对象, this-> HashTable :: HashTable(53); 在Visual Studio中强制重载的构造函数在调用对象上被调用。



一个初始化列表如

编译器禁止这个,这是一个坏习惯,无论你的编译器是否允许。 > HashTable :: HashTable :: HashTable():HashTable(53){} 被认为是正确的方式来完成我想做的事。


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()
    {
            HashTable(53);
    }

    HashTable::HashTable(int tableSize)
    {
            currentSize = tableSize;
            table.resize(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.

这篇关于从默认构造函数调用重载构造函数时维护对象状态信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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