 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matrix Guest
|
Posted: Mon Jun 27, 2005 10:46 am Post subject: Hi |
|
|
Hi,
I am new to this group.Would appreciate a little help. I have two
classes which are derived from the same parent. say class A and B
derived from C. I want to call the method of Class B from A and vice
versa, depending on a boolean variable. Also, assume this constraint
that I cannot construct a new object of A in B or vice versa. Any idea
on how to do this?
Thx in advance,
Ganesh Ram.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Guest
|
Posted: Mon Jun 27, 2005 5:18 pm Post subject: Re: Hi |
|
|
Hello Ganesh,
On Mon, 27 Jun 2005 10:46:45 UTC, "Matrix" <stganeshram (AT) gmail (DOT) com> wrote:
| Quote: | Hi,
I am new to this group.Would appreciate a little help. I have two
classes which are derived from the same parent. say class A and B
derived from C. I want to call the method of Class B from A and vice
versa, depending on a boolean variable. Also, assume this constraint
that I cannot construct a new object of A in B or vice versa. Any idea
on how to do this?
|
Okay, we have a base class C, and two separate classes derived from C,
called A and B. A and B have methods and data common to C and what each
of their respective (A and B) classes add.
What 'method' are you wanting to call in B from A? A and B are separate
and distinct classes and would yield different objects. A and B would need
some way to know about each other. They do not, unless all three classes
are derived linearly (C->B->A). You can then create an object from A that
has all of A, B, and C's methods and data. However, some methods may be
hidden by derived classes.
I think you are getting a few of the C++ concepts confused. Can you give
a more detailed example of what you want A, B, and C to have in common
and what uniqueness they should provide.
| Quote: | Thx in advance,
Ganesh Ram.
|
David
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Tue Jun 28, 2005 9:39 am Post subject: Re: Hi |
|
|
"Matrix" <stganeshram (AT) gmail (DOT) com> writes:
| Quote: | I am new to this group. Would appreciate a little help. I have two
classes which are derived from the same parent. say class A and B
derived from C. I want to call the method of Class B from A and vice
versa, depending on a boolean variable. Also, assume this constraint
that I cannot construct a new object of A in B or vice versa. Any idea
on how to do this?
|
Your description is a bit vague.
Please post some code (i.e. the definitions of classes A and B with
the declarations of the member functions needed to demonstrate your
problem) that you have tried to get working. Also tell us why your
attempt didn't work (e.g. if there were compiler errors).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
R.F. Pels Guest
|
Posted: Tue Jun 28, 2005 9:50 am Post subject: Re: Hi |
|
|
Matrix wrote:
| Quote: | I am new to this group.Would appreciate a little help. I have two
classes which are derived from the same parent. say class A and B
derived from C. I want to call the method of Class B from A and vice
versa, depending on a boolean variable. Also, assume this constraint
that I cannot construct a new object of A in B or vice versa. Any idea
on how to do this?
|
I'd love to see the actual problem, and I want to make a little bet that
this is a case where you modelled role behaviour through inheritance.
--
Ruurd
..o.
...o
ooo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tony_in_da_uk@yahoo.co.uk Guest
|
Posted: Tue Jun 28, 2005 10:41 am Post subject: Re: Hi |
|
|
No offense intended, but you're probably doing the wrong thing in your
class heirarchy, or your reasons for "I cannot construct a new object"
(sounds like the kind of stipulation a teacher might arbitrarily make).
Anyway, illustrating the concept:
class Cats_And_Dogs { ... };
class Dog : public Cats_And_Dogs
{
public:
void bark(bool full_moon) const
{
if (full_moon)
// how to Cat::meow?
else
cout << "woof" << endl;
}
};
class Cat : public Animal
{
public:
void meow(bool full_moon) const
{
if (full_moon)
// how to Dog::bark?
else
cout << "meow" << endl;
}
};
Given boths Cats and Dogs want to bark() and meow(), move these
function to Cats_And_Dogs, possibly as protected members called by
public Dog::bark() and Cat::meow(). You might prefer a public void
make_noise(bool prefer_bark, bool full_moon) in Cats_And_Dogs.
For a more sensible answer, please provide a more realistic description
of what you're trying to do, and why.
Cheers, Tony Delroy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|