Inheritance Problem

This is a discussion on Inheritance Problem within the C++ Programming forums, part of the General Programming Boards category; Oh no the big guy(Delphi) is BROKEN too, how sad. Code: interface type TNode = class protected fTest :String; public ...

  1. #16
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Oh no the big guy(Delphi) is BROKEN too, how sad.
    Code:
    interface
    type
      TNode = class
        protected fTest :String;
        public property Test: String read fTest;
      end;
      TLinkedNode = class(TNode)
        public procedure Test(Node: TNode);
      end;
    implementation
    procedure TLinkedNode.Test(Node: TNode);
    begin Node.fTest := 'Hello world!'; end;
    end.
    
    var
      Node  : TNode;
      Linked: TLinkedNode;
    begin
      Node := TNode.Create;
      Linked := TLinkedNode.Create;
      Linked.Test(Node);
      Writeln(Node.Test);
    end;
    I just figure it out this is as a matter of package/unit visibility, since C++ is only know straightforward name collision solver namespaces.

    However the problem can be solved in nasty way(which maybe possible in C++): If and if only there is an ability to access parameter's base class to call the non-overriden method -- IF the parameter IS LinkedNode, otherwise we don't care.
    EDIT:
    Sorry, I will just giving you my simplified abstract problem. I usually use Stack, Node, LinkedList, Point or Rectangles which in reality much much overcomplicated :P
    Last edited by audinue; 08-15-2009 at 01:40 PM.
    Just GET it OFF out my mind!!

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Delphi is broken too.
    C++ isn't stupid nor broken. You are putting it into use the wrong way.
    At this point, it seems that you just want to coerce it to work the way you want, and I say that's not a good idea. You should just quit C++ if that's your attitude.
    What you want is to make LinkedNode a friend of Node. This is exactly what Java and Delphi has done for you. But I doubt it's a good design overall.

    A better design, I think, is to have a common base class and derive your functionality to Node and LinkedNode. But Node and LinkedNode should be separate, ie LinkedNode should be a Node.
    No friends. No compile errors. No horrible workarounds.
    Last edited by Elysia; 08-15-2009 at 01:39 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.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by audinue
    I just figure it out this is as a matter of package/unit visibility, since C++ is stupid enough to know that but straightforward name collision solver namespaces.
    The second part of your sentence is unintelligible to me. This is just a matter of language rules depending on how language designers interpret theory and consider practice. I tend to think that C++'s interpretation is unnecessarily strict since the damage is done anyway once you have non-private member variables, but that does not mean that C++ "isn't mature enough to handle high level OOP concepts". You really should stop making such troll-like comments until you are more mature in your understanding of the programming language that you are commenting on.

    Quote Originally Posted by audinue
    However the problem can be solved in nasty way(which maybe possible in C++): If and if only there is an ability to access parameter's base class to call the non-overriden method -- IF the parameter IS LinkedNode, otherwise we don't care.
    The "access parameter's base class to call the non-overriden method" part is easy:
    Code:
    virtual void setNext(Node *next) {
        if(next) {
            next->Node::setPrevious(this);
        }
        Node::setNext(next);
    }
    If you actually want to have it behave depending on the actual type of the object that the next parameter points to, then you will incur the runtime penalty of a type check:
    Code:
    virtual void setNext(Node *next) {
        if(next && dynamic_cast<LinkedNode*>(next) == 0) {
            next->setPrevious(this);
        }
        Node::setNext(next);
    }
    Last edited by laserlight; 08-15-2009 at 01:57 PM. Reason: LinkNode -> LinkedNode
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Laserlight rocks! You rule!

    Thank you and sorry for my bad attitudes.
    I'm just stressed enough solving somekind of heavy AI program which is unsuitable for any language but C++ for performance controls and managements.

    Laserlight! Thank you!

    EDIT:
    Elysia's suggestion about separating Node interface, Node and the linked one is very good, if the states put in the implementing classes -- which leads to code rewriting and duplication.
    Last edited by audinue; 08-15-2009 at 02:29 PM.
    Just GET it OFF out my mind!!

Page 2 of 2 FirstFirst 12
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, 05:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 08:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 10:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21