简体   繁体   English

c ++链表到跳转列表转换

[英]c++ linked list to skiplist conversion

I want to convert a (simple linked) list into a skiplist which is derived from linked list. 我想将(简单链接)列表转换为从链表派生的跳转列表。 Inside the conversion ctor which gets a (linked-)list as param, i get a stackoverflow at *. 在获取(链接)列表作为参数的转换ctor中,我获得了*的堆栈溢出。 I just call that ctor from main once. 我只是从主要的那个叫ctor。 How is it possible that new SkipList is called loopwise? 如何以循环方式调用新的SkipList?

class SkipList : public List {
public:     
    SkipList(SkipListEl* first) : List (first){};

    SkipList(const List& sl){                           
        ListEl* fst = sl.getFirst();
        new SkipList(fst);         // * <- stackoverflow

        while(fst->hasNext()){
            new SkipListEl(*fst);
            fst = fst->getNext();
        }
    };

You need to review your basic C++ rules on dynamic object creation. 您需要查看有关动态对象创建的基本C ++规则。 new calls the constructor for the object you're creating. new调用您正在创建的对象的构造函数。 By calling new object in the constructor for object , you're getting an infinite loop (infinite recursion, actually) leading to no more stack space. 通过调用new object的构造函数object ,你得到一个无限循环(无限循环,实际上),导致没有更多的堆栈空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM