[英]Passing variable to constructor to a derived class gives garbage value, both the base and derived class's constructors have different parameters. Why?
The derived and base class's constructor both have different parameters.派生类和基类的构造函数都有不同的参数。
When passing a variable to the constructor of the derived class, it is giving garbage value when declaring an array of base class.当将变量传递给派生 class 的构造函数时,它在声明基本 class 的数组时会给出垃圾值。 Array size is not 'n' but some garbage value.
数组大小不是“n”,而是一些垃圾值。
class LinkedList{
public:
Node* head;
LinkedList()
{
cout << "c";
head=NULL;
}
};
class hashing : public LinkedList{
public:
int n;
hashing(int num)
{
this->n=num;
}
LinkedList* l = new LinkedList[n];
void iterateL()
{
for(int i=0; i<n; i++)
{
l[i].head=NULL;
cout << i << endl;
}
}
};
int main()
{
int n=7;
hashing op(n);
}
The loop inside hashing class is expected to run 7 times but after running 4 times it gives an error.散列 class 内的循环预计会运行 7 次,但在运行 4 次后会出现错误。
Okay, the problem is subtle here (to the point where I didn't see the issue at first).好的,这里的问题很微妙(到我一开始没有看到问题的地步)。
The problem is you're initializing using the value of n ( this->n
) during the in class member initialization.问题是您在 in class 成员初始化期间使用 n (
this->n
)的值进行初始化。
The problem is, in C++, those initializers are all run during member initialization, which is prior to the constructor body.问题是,在 C++ 中,这些初始化程序都在成员初始化期间运行,这是在构造函数主体之前。 Since you don't set the value of
this->n
until the constructor body, this->n
isn't initialized at all由于在构造函数主体之前没有设置
this->n
的值,所以this->n
根本没有初始化
In other words, what you have is the same as writing换句话说,你所拥有的和写作一样
hasing(int num): l(new LinkedList[n]) {
this->n = num;
}
This occurs because by default, all members are initialized in the order specified in the class, substituting default member initializers (the expression after the =
in the class definition) as appropriate.这是因为默认情况下,所有成员都按照 class 中指定的顺序进行初始化,并酌情替换默认成员初始化程序(class 定义中
=
之后的表达式)。
There are several solutions:有几种解决方案:
Using the member initializer使用成员初始化器
hashing(int num): n(num),l(new LinkedList[n])
{
}
Using the constructor body:使用构造函数体:
hashing(int num)
{
this->n = num;
this->l = new LinkedList[n];
}
In both of the above cases, I would not set the default member specifier.在上述两种情况下,我都不会设置默认成员说明符。 I would just declare the pointer.
我只想声明指针。
LinkedList* l; // No default member initializer
Finally, I would make sure you properly handle destruction, and copy/move assignment.最后,我会确保您正确处理破坏和复制/移动分配。
Your hashing
constructor should look like this:您的
hashing
构造函数应如下所示:
hashing(int num)
{
this->n=num;
l = new LinkedList[n]; // allocate once 'n' is known
}
otherwise, you are not allocating enough elements in l
.否则,您没有在
l
中分配足够的元素。 In fact, you are allocating n
elements, but since n
is not given a default value, it is garbage, and UB to use.实际上,您正在分配
n
元素,但是由于n
没有给定默认值,因此它是垃圾,并且要使用 UB。
The default value for l
can just be: l
的默认值可以是:
LinkedList* l = nullptr;
Of course, make sure to delete the memory in the destructor:当然,一定要删除析构函数中的memory:
~hashing()
{
delete [] l;
}
When the body of a constructor gets control all non-static members of the class are already constructed.当构造函数的主体获得控制时,class 的所有非静态成员都已构造。
So this data member所以这个数据成员
LinkedList* l = new LinkedList[n];
was already constructed using an indeterminate value of the data member n.已使用数据成员 n 的不确定值构造。
Rewrite the constructor the following way用以下方式重写构造函数
hashing(int num) : n( num )
{
}
Pay attention to that there is no sense to declare the class hashing as a derived class of LinkedList because simultaneously you are also using the composition请注意,将 class 散列声明为 LinkedList 的派生 class 是没有意义的,因为同时您也在使用组合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.