是在创建对象时初始化的类中的成员值吗? [英] Is member value in the class initialized when an object is created?

查看:113
本文介绍了是在创建对象时初始化的类中的成员值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个哈希类:

struct hashmap {
  void insert(const char* key, const char* value);
  char* search(const char* key);
 private:
  unsigned int hash(const char* s);
  hashnode* table_[SIZE]; // <--
};

由于insert()需要在插入一个新对时检查table [i]是否为空我需要表中所有的指针在启动时设置为NULL。

As insert() need to check if table[i] is empty when inserting a new pair, so I need all pointers in the table set to NULL at start up.

我的问题是,这个指针数组 table _ 自动初始化为零,或者我应该手工使用循环在构造函数中将数组设置为零。

My question is, will this pointer array table_ be automatically initialized to zero, or I should manually use a loop to set the array to zero in the constructor?

推荐答案

table _ 数组将在您当前的设计中被未初始化,就像你说 int n; 。但是,您可以在构造函数中初始化数组(并因此将每个成员初始化为零):

The table_ array will be uninitialized in your current design, just like if you say int n;. However, you can value-initialize the array (and thus zero-initialize each member) in the constructor:

struct hash_map
{
    hash_map()
    : table_()
    {
    }

    // ...
};

这篇关于是在创建对象时初始化的类中的成员值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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