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. 6 3762
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
"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?
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.
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
"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;
}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matan Nassau |
last post by:
This question is a little embarrassing... I have a boolean expressions
interpreter with its composite (see Go4's "Design Patterns" book).
here is a simple leaf of the composite:
class Constant :...
|
by: Michael Andersson |
last post by:
Given a set of classes
class A { enum [ ID = 0x0001} };
class B { enum [ ID = 0x0002} };
class B { enum [ ID = 0x0004} };
I wish to generate a composite class, perhaps using something like...
|
by: AshifToday |
last post by:
this was my and my frineds little project in earlier classes,
the program seperates the composite and prime numbers in two sections
of the screen
=====================
/*
This program has...
|
by: AndrewF |
last post by:
Hi there.
I am royally stuck with this so any help would be greatly appreciated.
Basically I have a set of classes that create composite controls for
the user. One of these is an upload object...
|
by: multiformity |
last post by:
Ok, so I have gone off and documented the lifecycle of a page with a
custom composite control on it. You can find that document here:
http://www.ats-engineers.com/lifecycle.htm
Now, I am...
|
by: Beavis |
last post by:
I hate to repost a message, but I am still at the same point where I
was when I originally posted, and hopefully someone else will see this
one...
Ok, so I have gone off and documented the...
|
by: pkpatil |
last post by:
Hi,
Can a private composite object in a class access private or protected
members of base class?
For e.g.
class composite {
void memberFunction();
};
|
by: lwhitb1 |
last post by:
Does anyone have any input on setting up my CAB application so that the
application is thread safe, and cached appropiately? I read that this
can be managed through Services, and dynamic injection....
|
by: shapper |
last post by:
Hello,
I am working in a class library with various custom controls.
In which cases should a control inherit Control, WebControl and
CompositeControl classes?
And when should a custom...
|
by: Peter Morris |
last post by:
I am not sure what you are asking. You seem to be asking how to implement a
plain IEnumerable on a composite structure, but then your example shows a
flat structure using "yield". Your subject...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |