473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

class interaction

Dan
How can I let two classes call a method from each other,
like in this example?
class A {
B* myB;
void doSomething() {
myB->doSomething();
}
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};

Thanks,
Dan
Jul 22 '05 #1
6 3206
Dan wrote:
How can I let two classes call a method from each other,
like in this example?
class A {
B* myB;
void doSomething() {
myB->doSomething();
}
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};

Thanks,
Dan


class B;

class A {
B* myB;
inline void doSomething();
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};
inline void A::doSomething() {
myB->doSomething();
}

And you perfect endless loop is ready, but it will not be endless (eats up
the call-stack).

--
Attila aka WW
Jul 22 '05 #2

"Dan" <da***********@hotmail.com> wrote in message
news:7d**************************@posting.google.c om...
How can I let two classes call a method from each other,
like in this example?
class A {
B* myB;
void doSomething() {
myB->doSomething();
}
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};

Thanks,
Dan


class B;

class A {
B* myB;
public:
void doSomething(); //I'm assuming you want this to be public
};

class B {
A* myA;
public:
void doSomething() {myA->doSomething();}
};

void A::doSomething()
{
myB->doSomething();
}

Regards,
Sumit.
Jul 22 '05 #3
Dan wrote:
How can I let two classes call a method from each other,
like in this example?
class A {
B* myB;
void doSomething() {
myB->doSomething();
}
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};

Thanks,
Dan


class A{
B * m_b;
public:
A( B * b ):m_b( b ){};
void b_do(){
m_b->doSomething();
}
void doSomething(){};
};

class B{
A * m_a;
public:
B( A * a ):m_a( a ){};
a_do(){
m_a->doSomething();
}
void doSomething(){};
}


Jul 22 '05 #4
On Wed, 04 Feb 2004 03:36:04 -0800, Dan wrote:
How can I let two classes call a method from each other,
like in this example?

class B;
class A {
B* myB;
void doSomething() {
myB->doSomething();
}
void doSomething();
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};


void A::doSomething()
{
myB->doSomething();
}

Note the order. Class B is forward declared, therefore class A can use a
pointer to B. It cannot (yet) use B itself, therefore we moved the
implementation of A::doSomething down.

HTH,
M4

Jul 22 '05 #5
Dan wrote:

How can I let two classes call a method from each other,
like in this example?

http://www.parashift.com/c++-faq-lit...html#faq-38.11

Brian Rodenborn
Jul 22 '05 #6
"Dan" <da***********@hotmail.com> wrote
How can I let two classes call a method from each other,


A number of replies were already given on how to do it. Let me add this: unless
the classes are intended to be tightly coupled and jointly represent a single
interface, as with containers and their iterators, you should NOT introduce
circular dependencies in your code. That makes the code difficult to test,
maintain, and read. If you ever encounter an APPARENT need to have circular
dependencies, reconsider the problem in light of a third class that acts as a
coordinator between the two. That way, you have one class that depends on two
independent classes.

Claudio Puviani
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BJS | last post by:
Sorry for the cross-posting, but based on the number of people I have seen ask for a solution to this problem, I hope by cross-posting this, that it will help a lot of people out of a common...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
by: venk | last post by:
Hi, given below is my interaction with the interpreter.... In one case, i have created the class method using the "famous idiom"... and in the other, i have tried to create it outside the class...
3
by: Aaron Queenan | last post by:
I have a form which performs some asynchronous code. I want to display a wait cursor when it starts, and hide the wait cursor when it has completed. This part works fairly well, using: ...
3
by: Trammel | last post by:
Hi, I recently upgraded to VB.net from VB6.. and woah... I feel lost :¬O One of my reasons for upgrading is I was told that VB.net can do class inheritance and subclassing easier. ...
7
by: Paul | last post by:
Hello, I'm coming from a PHP background and am working on a large-scale VB.NET project. One of the cornerstones of this project is a reusable form class, to standardize our web forms. My goal...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
4
by: vivek | last post by:
I am new to flash and want someone to guide me, Is it possible to create a UI entirely in Flash and that will inetract with C# components (backend) and these components will in return interact...
1
by: dwij2u | last post by:
Hi All, Can someone guide me on how we can modify the below script so that no user interaction is required to feed the password when the script is run, current scenario is like if I try to login...
3
by: Angus | last post by:
I am designing an FTP server and with FTP commands are dispatched on one port and the actual data transmission on another. So I thought I would have a CFTPControl class for handlng the commands...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
tracyyun
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.