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

Composite classes

P: n/a
I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:

class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}

Is there a way for objB to invoke a method defined in class C? For
instance,

void B::functionB(void)
{
objC.functionC(void);
}

What is the best way to "link" B and C?

Thanks.

Aug 5 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Aug 5, 8:57 am, gccntn <gcc...@yahoo.comwrote:
I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:

class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...

}

Is there a way for objB to invoke a method defined in class C? For
instance,

void B::functionB(void)
{
objC.functionC(void);

}

What is the best way to "link" B and C?

Thanks.
Hi,

One idea is something like that:
void B::functionB(C& objC)
{
objC.functionC();
}
Anyway, you should pass an object or reference to an object of class
C. of course you can define a local C object inside functionB:
void B::functionB()
{
C objC;
objC.functionC();
}

It depends on your problem.

Regards,
Saeed

Aug 5 '07 #2

P: n/a
"gccntn" <gc****@yahoo.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:

class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}

Is there a way for objB to invoke a method defined in class C? For
instance,

void B::functionB(void)
{
objC.functionC(void);
}

What is the best way to "link" B and C?
It depends on what it is you are actually trying to achieve. If B needs to
invoke a method of C, then perhaps C should be a member of B. You have many
alternatives though.

( following is all untested code )
1. Have the constructor of B take a reference to C and store it.

class B
{
public:
B( C& c ): c(c) {}
void functionB();
private:
C& c;
}

void B::functionB()
{
c.functionC();
}

2. Have an initialization function for B take a pointer to C and store it.

class B
{
public:
B( ): c( NULL ) {}
void Init( C* cp ) { c = cp; }
void functionB();
private:
C* c;
}

void B::functionB()
{
if ( c )
c->functionC();
}

3. Best IMO, have B create it's own C

class B
{
public:
void functionB();
private:
C c;
}

void B::functionB()
{
c.functionC();
}

4. Have functionB accept a reference to C.

class B
{
public:
void functionB( C& c);
}

void B::functionB( C& c )
{
c.functionC();
}

5. Other less desirable methods (public instance of C, etc..)

The main quesiton is, WHY does B need to call a method of C?


Aug 5 '07 #3

P: n/a
On Aug 5, 3:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"gccntn" <gcc...@yahoo.comwrote in message

news:11**********************@k79g2000hse.googlegr oups.com...


I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:
class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}
Is there a way for objB to invoke a method defined in class C? For
instance,
void B::functionB(void)
{
objC.functionC(void);
}
What is the best way to "link" B and C?

It depends on what it is you are actually trying to achieve. If B needs to
invoke a method of C, then perhaps C should be a member of B. You have many
alternatives though.

( following is all untested code )
1. Have the constructor of B take a reference to C and store it.

class B
{
public:
B( C& c ): c(c) {}
void functionB();
private:
C& c;

}

void B::functionB()
{
c.functionC();

}

2. Have an initialization function for B take a pointer to C and store it.

class B
{
public:
B( ): c( NULL ) {}
void Init( C* cp ) { c = cp; }
void functionB();
private:
C* c;

}

void B::functionB()
{
if ( c )
c->functionC();

}

3. Best IMO, have B create it's own C

class B
{
public:
void functionB();
private:
C c;

}

void B::functionB()
{
c.functionC();

}

4. Have functionB accept a reference to C.

class B
{
public:
void functionB( C& c);

}

void B::functionB( C& c )
{
c.functionC();

}

5. Other less desirable methods (public instance of C, etc..)

The main quesiton is, WHY does B need to call a method of C?- Hide quoted text -

- Show quoted text -

"The main quesiton is, WHY does B need to call a method of C?"

Good question. Say A (the composite class) is Car, B is Controls and C
is Engine. As I envision this, Controls must be able to tell Engine
when to Start, Accelerate, Stop and so on. However, please let me know
if anybody wants to suggest a different design. I'm open to
suggestions.

Thanks for all the good tips.

Aug 6 '07 #4

P: n/a
Hi!

gccntn schrieb:
"The main quesiton is, WHY does B need to call a method of C?"

Good question. Say A (the composite class) is Car, B is Controls and C
is Engine. As I envision this, Controls must be able to tell Engine
when to Start, Accelerate, Stop and so on. However, please let me know
if anybody wants to suggest a different design. I'm open to
suggestions.
Sound like the "have B have a pointer/reference to C" solution to me.

Frank
Aug 16 '07 #5

P: n/a
"gccntn" <gc****@yahoo.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
On Aug 5, 3:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"gccntn" <gcc...@yahoo.comwrote in message

news:11**********************@k79g2000hse.googleg roups.com...


>I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:
class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}
Is there a way for objB to invoke a method defined in class C? For
instance,
void B::functionB(void)
{
objC.functionC(void);
}
What is the best way to "link" B and C?

It depends on what it is you are actually trying to achieve. If B needs
to
invoke a method of C, then perhaps C should be a member of B. You have
many
alternatives though.

( following is all untested code )
1. Have the constructor of B take a reference to C and store it.

class B
{
public:
B( C& c ): c(c) {}
void functionB();
private:
C& c;

}

void B::functionB()
{
c.functionC();

}

2. Have an initialization function for B take a pointer to C and store
it.

class B
{
public:
B( ): c( NULL ) {}
void Init( C* cp ) { c = cp; }
void functionB();
private:
C* c;

}

void B::functionB()
{
if ( c )
c->functionC();

}

3. Best IMO, have B create it's own C

class B
{
public:
void functionB();
private:
C c;

}

void B::functionB()
{
c.functionC();

}

4. Have functionB accept a reference to C.

class B
{
public:
void functionB( C& c);

}

void B::functionB( C& c )
{
c.functionC();

}

5. Other less desirable methods (public instance of C, etc..)

The main quesiton is, WHY does B need to call a method of C?- Hide quoted
text -

- Show quoted text -


"The main quesiton is, WHY does B need to call a method of C?"

Good question. Say A (the composite class) is Car, B is Controls and C
is Engine. As I envision this, Controls must be able to tell Engine
when to Start, Accelerate, Stop and so on. However, please let me know
if anybody wants to suggest a different design. I'm open to
suggestions.

Thanks for all the good tips.
A car HAS controls. Being such, C should be inside B.

I would go with method 3. B has it's own C.

class Car
{
public:
void Acceleratre( int Speed ) { C.Accelerate( Speed ); }
private:
Controls control;
}

Aug 16 '07 #6

P: n/a
Jim Langston wrote:
>
A car HAS controls. Being such, C should be inside B.

I would go with method 3. B has it's own C.
Hmmmm, at this level, I'm not sure I agree. A car has an engine,
windows, trunk, radio, etc. Now the engine has it's controls (starter,
throttle), a window has it's controls (crank or button), and the radio
has it's own controls (tuner, volume, etc). These controls are all
accessible from the car, but they are not the car's controls. That is,
they don't control the car, but rather a component of the car. I
probably would not model the controls as a separate object at all. Why
not just tell the engine to turn on, accelerate, etc directly? If you
try to get too close to reality things get much more complex without a
gain to offset it.

Having said all that, if you still wanted the car to have controls, then
I might consider using the observer pattern with the engine observing
the controls.

joe
Aug 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.