Composite classes

G

gccntn

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.
 
A

Alf P. Steinbach

* gccntn:
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?

Let the B constructor take a reference to C, and store the address of
the C object in a B member variable.
 
S

Saeed Amrollahi

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
 
J

Jim Langston

gccntn said:
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?
 
G

gccntn

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.
 
F

Frank Birbacher

Hi!
"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
 
J

Jim Langston

gccntn said:
"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;
}
 
J

Joe Greer

Jim said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,371
Latest member
TroyHursey

Latest Threads

Top