0

I have

class A{
}
class B : virtual public A{
  extra property 1
}
class C : virtual public A{
  extra property 2
}
class D : virtual public A{
}
class E : virtual public B, virtual public C, virtual public D{
  extra property 3
}

And here's my linked list's insert function:

template <typename T>
class LinkedList {
   Node<T> *start;
   Node<T> *current;
public:
void insert (T newElement)
{
  if ( isEmpty() )
  {
     insertToStart(newElement);
  }
  else
  {
     Node<T> *newNode = new Node<T>;
     newNode->info = newElement;
     newNode->next = NULL;
     current = start;
     while (current->next != NULL)
     {
        current = current->next;
     }
     current->next = newNode;
  }
}

Now I would like to create a single linked list that will store all instances of B, C and D without losing the additional attribute of each child class. How can I achieve this using pointer? I've tried the solution provided here, but it gives me this error: taking address of temporary [-fpermissive].

Linkedlist<A*> list;
B b;
list.insert(&b);
Community
  • 1
  • 1
user3049955
  • 25
  • 1
  • 4
  • The solution you've linked to uses `std::list`, [and works on coliru](http://coliru.stacked-crooked.com/a/a125e012d59d1b35). What's your `LinkedList`? It's not the `std::` one, as `insert()` doesn't have an overload which takes only one parameter. – JBL Jan 10 '14 at 16:38
  • I've just edited the post to show my linked list's public function! – user3049955 Jan 10 '14 at 16:52
  • On which line does it errors? The `insert` one? Or another? (Especially check if it errors on the `insert`line that it's not just part of the call stack). – JBL Jan 10 '14 at 17:01

1 Answers1

0

The following piece of code compiles fine with gcc 4.8:

#include <list>

class A
{
};

class B : virtual public A
{
};

int main()
{
    std::list<A*> l;

    B* b = new B();
    l.push_back(b);

    B bb;
    l.push_back(&bb);

    return 0;
}
A. Lefebvre
  • 139
  • 4
  • No more compilation errors, but somehow the mechanism is not really functioning well. When I display the elements in l, it returns some strange garbage values like this one: 0x3d0fe0 instead of listing the attributes of the object. – user3049955 Jan 10 '14 at 16:55