简体   繁体   English

将变量传递给构造函数到派生的 class 会给出垃圾值,基类和派生类的构造函数都有不同的参数。 为什么?

[英]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.

相关问题 当派生类和基类都具有参数化构造函数时,如何初始化派生类的对象? - How to initialize an object of derived class when the derived class and base class both have parameterized constructors? 如果不为具有不同签名的不同派生构造函数调用基类的构造函数,会发生什么情况? - What happens if you don't call a base class's constructor for different derived constructors with different signatures? 基类和派生类中的构造方法 - Constructor in base and derived class 基本构造函数继承到派生类? - Base constructors inherited to derived class? 如何从派生类的基类访问变量的值? - How to access variable's value from base class in derived class? 将非构造函数参数变量从派生类传递给基类构造函数会导致奇怪的行为 - Passing a non-constructor parameter variable to the base class constructor from a derived class causes weird behaviour 如果派生类没有构造函数,基类构造函数会受到谴责吗? - Is the base class constructor excuted if the derived class does not have a constructor? 在创建派生类实例时提供基类构造函数参数 - Providing base class constructor parameters when creating a derived class instance 为什么先调用基类构造函数然后派生类构造函数 - why first calling base class constructor and then derived class constructor 基类指针仅获得派生类变量值,而不得到基类变量值,为什么? - Base Class Pointer only getting a Derived class variable value and not the base class variable value why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM