Thread: Derived datastructur

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Derived datastructur

    Hi,

    What is the best approach to this data structure?
    I have two classes that are almost the same, inn classA I have a protected/private struct that differs slightly from the struct in classB.

    What I need is something like a BaseClass which hold the generalization, the basic struct and some common functions, and two derived classes which extend the BaseClass and its struct and functions.
    Furthermore the DerivedClassA has a pointer var to DerivedClassB...
    Yes, this seems messy… I know! This is a school assignment…
    I’m not too sure about the extending of the struct stuff… Union?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should not need a union to do this. You need to inherit the BaseClass and then add the extra elements in the derived classes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Txn for replay!
    What confuses me though is the struct, the assignment implies that there is a method for extending the struct inside the base class… I might be on completely of course here..

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you shouldn't be allowed to access a struct inside the private area, so no external code would know what the struct looks like.

    Particularly when it comes to derived classes, there should never be direct access to the data inside the class, as that is bound to fail when you introduce a new derived class. [Of course, you also can't use any of the extra data without checking if the class supports that - you can either write virtual functions that return "nothing" when you ask for the extra data that isn't there, or some sort of "isa" function to check if the derived class supports some certain functionality].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-03-2009, 03:00 PM
  2. Deleting derived classes
    By *DEAD* in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2008, 12:37 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM