By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

class hierarchy

P: n/a
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


Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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.

Jul 23 '05 #2

P: n/a
On 2005-03-25, Ilia Poliakov <ip*****@web.de> 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/
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.