user972851
user972851

Reputation: 177

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 *. I just call that ctor from main once. How is it possible that new SkipList is called loopwise?

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

Upvotes: 0

Views: 425

Answers (1)

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29579

You need to review your basic C++ rules on dynamic object creation. new calls the constructor for the object you're creating. By calling new object in the constructor for object, you're getting an infinite loop (infinite recursion, actually) leading to no more stack space.

Upvotes: 2

Related Questions