class hierarchy

Discussion in 'C++' started by Ilia Poliakov, Mar 25, 2005.

  1. I have a following class hierarchy, but I dont know if it's right built:

    //------------------------------ begin of example
    1 ------------------------------
    class InnerClass_A
    { .... };
    class InnerClass_B : public InnerClass_A
    { .... };

    class Main
    { ... };

    class MainClass_A : public Main
    {
    InnerClass_A *m_pMember_A;

    public:
    MainClass_A (InnerClass_A *pMember) {
    m_pMember_A = pMember;
    };

    InnerClass_A *getMember() {
    return m_pMember_A;
    };
    };

    class MainClass_B : public Main
    {
    InnerClass_B *m_pMember_B;

    public:
    MainClass_B (InnerClass_B *pMember) {
    m_pMember_B = pMember;
    };

    InnerClass_B *getMember() {
    return m_pMember_B;
    };
    };
    //------------------------------ end of example
    1 ------------------------------

    May be I can change it to this:


    //------------------------------ begin of example
    2 ------------------------------
    class MainClass_A : public Main
    {
    InnerClass_A *m_pMember;

    public:
    MainClass_A (InnerClass_A *pMember) {
    m_pMember = pMember;
    };


    InnerClass_A *getMember() {
    return m_pMember;
    };
    };

    class MainClass_B : public MainClass_A
    {
    public:
    MainClass_B (InnerClass_B *pMember) {
    m_pMember = pMember;
    };


    InnerClass_B *getMember() {
    return (InnerClass_B *)m_pMember;
    };
    };
    //------------------------------ end of example
    2 ------------------------------

    I'm new to C++ and need your advice: which version is better or may be both
    are bad....

    --
    _____________
    mfG Ilia Poliakov
     
    Ilia Poliakov, Mar 25, 2005
    #1
    1. Advertising

  2. Ilia Poliakov

    BigBrian Guest

    Ilia Poliakov wrote:
    > I have a following class hierarchy, but I dont know if it's right

    built:
    >
    > //------------------------------ begin of example
    > 1 ------------------------------
    > class InnerClass_A
    > { .... };
    > class InnerClass_B : public InnerClass_A
    > { .... };
    >
    > class Main
    > { ... };
    >
    > class MainClass_A : public Main
    > {
    > InnerClass_A *m_pMember_A;
    >
    > public:
    > MainClass_A (InnerClass_A *pMember) {
    > m_pMember_A = pMember;
    > };
    >
    > InnerClass_A *getMember() {
    > return m_pMember_A;
    > };
    > };
    >
    > class MainClass_B : public Main
    > {
    > InnerClass_B *m_pMember_B;
    >
    > public:
    > MainClass_B (InnerClass_B *pMember) {
    > m_pMember_B = pMember;
    > };
    >
    > InnerClass_B *getMember() {
    > return m_pMember_B;
    > };
    > };
    > //------------------------------ end of example
    > 1 ------------------------------
    >
    > May be I can change it to this:
    >
    >
    > //------------------------------ begin of example
    > 2 ------------------------------
    > class MainClass_A : public Main
    > {
    > InnerClass_A *m_pMember;
    >
    > public:
    > MainClass_A (InnerClass_A *pMember) {
    > m_pMember = pMember;
    > };
    >
    >
    > InnerClass_A *getMember() {
    > return m_pMember;
    > };
    > };
    >
    > class MainClass_B : public MainClass_A
    > {
    > public:
    > MainClass_B (InnerClass_B *pMember) {
    > m_pMember = pMember;
    > };
    >
    >
    > InnerClass_B *getMember() {
    > return (InnerClass_B *)m_pMember;
    > };
    > };
    > //------------------------------ end of example
    > 2 ------------------------------
    >
    > I'm new to C++ and need your advice: which version is better or may

    be both
    > are bad....
    >


    It depends on what you're trying to do. However they both *are* bad
    because you should be using initilizer lists.
     
    BigBrian, Mar 25, 2005
    #2
    1. Advertising

  3. On 2005-03-25, Ilia Poliakov <> wrote:
    > I have a following class hierarchy, but I dont know if it's right built:


    Apart from the previous comment about initializer lists:
    (1) obviously, you need a copy constructor, assignment and a destructor
    (2) You can make a parallel class heirarchy if you like, but you don't
    have to. The simplest versionis just something like:

    class Main {
    InnerClass_A* m_pMember_A;
    Main(const Main&); // no copying allowed
    Main& operator=(const Main&);
    public:
    Main(InnerClass_A* x): m_pMember_A(x) {}
    ~InnerClass_A() { delete m_pMember_A; }
    };

    or you could do something like reference counting or deep copying if you
    want to allow that, e.g.

    Main(const Main& x){
    InnerClass_A* tmp = new InnerClass_A(*m_pMember_A);
    delete m_pMember_A
    m_pMember_A = tmp;
    }
    Main& operator=(const Main& x){
    Main tmp(x); swap(tmp.m_pMember_A,m_pMember_A); return *this;
    }

    and also implement assignment in terms of the copy constructor for innerclass.

    You don't necessarily need a version of Main for B, because you can store
    InnerClass_B pointers in the above version of Main.

    Also, you could have a class heirarchy for Main, but it doesn't have to
    contain a class for each class in the heirarchy of InnerClassA. You can vary
    the heirarchy of your outer classes indendently. See the "bridge pattern" in
    Design patterns or try and find it on the net.

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, Mar 25, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mukund Patel
    Replies:
    0
    Views:
    1,737
    Mukund Patel
    Dec 31, 2004
  2. Avi Abrami
    Replies:
    6
    Views:
    493
  3. Gael
    Replies:
    2
    Views:
    434
    Andy Fish
    Dec 10, 2003
  4. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    465
    H.MuthuKumaraRajan
    Feb 4, 2004
  5. E11
    Replies:
    1
    Views:
    4,848
    Thomas Weidenfeller
    Oct 12, 2005
Loading...

Share This Page