Thread: Inheritance Problem

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Inheritance Problem

    What the hell is going on here??

    Is it language bug or my brain exploded already?

    I tried to compile this on many compiler but they gave me the same error code.

    Code:
    class Node {
    protected:
      Node *next;
      Node *previous; // error: `Node*Node::previous' is protected
    public:
      virtual Node *getNext(void) const { return next; }
      virtual void setNext(Node *next) { this->next = next; }
      virtual Node *getPrevious(void) const { return previous; }
      virtual void setPrevious(Node *previous) { this->previous = previous; }
    };
    
    class LinkedNode : public Node // See? LinkedNode inherits the Node!!
                                   // It supposed be able to access protected members of Node!!
    {
    public:
      virtual void setNext(Node *next) {
        // within this context
        if(next) { next->previous = this; }
        Node::setNext(next);
      }
      virtual void setPrevious(Node *previous) {
        if(previous) { previous->next = this; }
        Node::setPrevious(previous);
      }
    };
    Just GET it OFF out my mind!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't be mucking about with other classes' protected members. If you had passed in a LinkedNode object, then LinkedNode::setNext would be able to deal with it. But it can't handle a Node object.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Why it can't?
    Just GET it OFF out my mind!!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You seem to misunderstand inheritance. Internally, you may access your base protected members, but not externally! In other words, you can't expect to take an argument and then access its internal components.
    A class only has access to private and protected members in its own instance. If you do:

    this->next = something;
    or
    next = something;

    It would work, but if you would do

    LinkedNode NewNode;
    NewNode.next = something;

    you would get a compile error, because you are accessing another instance's protected members.
    Last edited by Elysia; 08-15-2009 at 11:14 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    Why it can't?
    Inheritance means that your new class has a copy of the base class inside it. It does not grant you full powers over other classes.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    It does not grant you full powers over other classes.
    That's what "friends" do, in case you are wondering...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    By defining friends class means breaking the concept for sure.

    Since Node is not associated to LinkedNode.

    However I can implement similar code without error and works as it supposed to do in another languageS.

    I think C++ isn't mature enough to handle high level OOP concepts.

    Argh! This is insane. I'LL just use struct instead, just like prefixing everything to replace namespaces in C.

    Why great power, speed and libraries lies in C++ only?
    Just GET it OFF out my mind!!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by audinue View Post
    I think C++ isn't mature enough to handle high level OOP concepts.
    This is not true. You are failing to grasp basic OOP concepts. It would (should not!) work in any other languages either.
    However, it seems that LinkedNode is unnecessary. Node has all the basic components that the list needs and LinkedNode is just a repetition of what you've added in Node.
    Last edited by Elysia; 08-15-2009 at 12:12 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    By defining friends class means breaking the concept for sure.

    Since Node is not associated to LinkedNode.

    However I can implement similar code without error and works as it supposed to do in another languageS.

    I think C++ isn't mature enough to handle high level OOP concepts.

    Argh! This is insane. I'LL just use struct instead, just like prefixing everything to replace namespaces in C.

    Why great power, speed and libraries lies in C++ only?
    I don't think any of this makes sense.

    If you had written your setNext/setPrevious as part of Node instead of LinkedNode, then not only would it compile, but you would be obeying OOP precepts (instead of what you are trying to do, which not only doesn't compile, but disobeys OOP precepts).

    I guess the question is: what should be different about LinkedNode? What's the "added" feature? (My guess, just based on the name, would be "the links" -- but in that case next and previous wouldn't be part of the base class in the first place.)

  10. #10
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    The first Node is plain Node without automatic linking which Linked one do.

    However LinkedNode "is-a" Node which automate the link process (which technically adding new processes which may or may not required by the client).

    Their behaviour is totally different even though they are the same (polymorphism).

    And their responsibilities is quiet enough clear to be separated with such relationship.

    Try to compile this (Java):
    Code:
    class Node { protected String test; }
    
    class LinkedNode extends Node {
      void test(Node node) { node.test = "Hello world!"; }
    }
    
    //in main
    Node node = new Node();
    LinkedNode linked = new LinkedNode();
    linked.test(node);
    System.out.println(node.test);
    Just GET it OFF out my mind!!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Java is broken.
    I suggest you make LinkedNode accept LinkedNodes, and not Nodes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    The first Node is plain Node without automatic linking which Linked one do.

    However LinkedNode "is-a" Node which automate the link process (which technically adding new processes which may or may not required by the client).

    Their behaviour is totally different even though they are the same (polymorphism).

    And their responsibilities is quiet enough clear to be separated with such relationship.
    If you say so, I guess. What you say above makes no sense to me (I have no idea what "automate the link process" could mean, even a little bit), but if there's a difference between the two in your code that doesn't appear in what you're showing us, then fine.

    In that case, however, something like this would follow OOP style:
    Code:
    virtual void setNext(Node *next) {
        if (next) {
            next.setPrevious(this);
        }
        this.next = next; //this means that your choice of parameter names sucks
    }
    Presumably the call to next.setPrevious is okay here and won't get us into a big old loop, since you claim that Nodes don't do automatic linking, so presumably it won't try to also set the next pointer of our current object otherwise we'll be here all day.

    EDIT TO ADD: Also, this means that if Node *next is actually something derived, then we will probably do the right thing by calling the override function instead of the base-class function. (Although, as mentioned, if next is really a LinkedNode, we will be in an infinite recursion kind of thing, which is another reason why I don't think your class design makes sense.)
    Last edited by tabstop; 08-15-2009 at 12:55 PM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A totally oblivious question, but: why don't you use std::set?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    A totally oblivious question, but: why don't you use std::set?
    Probably because he doesn't seem to want automatic sorting, or a requirement that a number can appear only once.

    Now if you want to ask why he doesn't use std::list, then go ahead.

    (Although the answer to that is going to be either (a) for learning purposes, or (b) look at all that overhead, going by the OPs signature.)

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *shrug* Just asking due to the problems the OP is facing getting a linked list design working. Should you bother if you can get away with using something that already exists?
    Perhaps. But it doesn't hurt to ask.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance problem
    By logicwonder in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2006, 10:14 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 09:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM