1

I have a skeleton of a project I need to implement a Doubly Linked List (no using stl) and the way the class is implemented is to inherit all is methods from a struct like so:

struct IDoubleList { 
virtual IDoubleNode * getHead() = 0;
virtual IDoubleNode * getTail() = 0;
virtual void setHead(IDoubleNode * head) = 0;
virtual void setTail(IDoubleNode * tail) = 0;
virtual void addBack(int value) = 0;
};

class DoubleList : public IDoubleList {
public:
virtual IDoubleNode * getHead();
virtual IDoubleNode * getTail();
virtual void addBack(int value);
virtual void setHead(IDoubleNode * head);
virtual void setTail(IDoubleNode * tail);
private:
DoubleNode* m_Head;
DoubleNode* m_Tail;
};

As you can getters and setters use the struct, not the class to return/pass pointers. My question is how can I use the methods in he object m_Tail is pointing to. I tried using m_Tail.setNext(newNode); where setNext is method in the DoubleNode class but that says the expression must have a class type.

Also when I return/pass a DoubleNode* should I be casting or something to IDoubleNode*? or maybe its the other way around?

PS been a while since ive used C/C++, maybe I'm forgetting something about function pointers? idk so lost right now

Thanks in advanced, let me know if you need any more info

3
  • 1
    There is no such thing as a distinction between class pointers and struct pointers. (That wouldn't seem to answer your question, but it should be commented to the headline). Commented Jan 31, 2012 at 23:44
  • Why have you defined an interface for which there is a single implementation? The question is a bit rethorical, if there is only one implementation, creating an interface (pure abstract class) will add complexity and offer no benefit. Reconsider your design. Commented Feb 1, 2012 at 0:39
  • read my post, i have the skeleton i can only implement classes given to me nothing else. Commented Feb 1, 2012 at 1:39

2 Answers 2

1

First of all, it should be m_Tail->setNext(newNode), not m_Tail.setNext(newNode) because m_Tail is a pointer. You could also do (*m_Tail).setNext(newNode), the point is that you have to dereference the pointer somehow.

No, you do not need to cast a pointer from a DoubleList to IDoubleList when passing it to a function expecting an IDoubleList. This is because every DoubleList is a IDoubleList, so no casting is required. Also, the only difference between a struct and a class is the default access level of the members (public for a struct, private for a class).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot, i knew there was definitely a way to use a dot operator. for some reason using -> was giving me a error:expression must have pointer-to-class type
@AnkurVashi -> operator can only be with pointers pointing to an object. For example, foo obj;, obj is not pointer to an object of type foo but the object itself. So, you have to use . operator to access its members.
I think you were doing something wrong with your syntax somewhere; if (*a).b() works, then a->b() should also work.
0

First, you should make IDoubleNode a base class of your DoubleNode (if you haven't already). Then you need to dynamic_cast<DoubleNode*>(ptr) in the methods taking a IDoubleNode* to get hold of your DoubleNode (and e.g. throw an exception if this fails). Also, make sure you use pointer notation when accessing members of pointers, i.e. ptr->member instead of ptr.member: the latter notation only works for objects. You could use (*ptr).member but this is unnecessarily contrived.

Finally, don't forget to tell your instructor to set reasonable assignments next time because this is an entirely pointless exercise: this is bad example for using object oriented approaches (you can subvert type-safety via the base class interface) and it is pointless to use object oriented approaches in a data structures class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.