Question

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();
        }
    };
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top