PDA

View Full Version : How to send information between two classes where one is instantiated from the other?



falconium
28th March 2011, 01:42
#include "apple.h"
class Basket
{
...
int number_of_apples;
Apple instantiatedApple;
}


Sorry about asking this general object-oriented question, but I could not find relevant pages with google.

The question is simple, but I cannot figure out the answer...

How to send information between two classes where one class is instantiated inside the other?

Example:


class Apple
{
void Apple::methodinsideapple()
{
(how would you get current number_of_apples in basket???????)
}
}


I have lots of QDomDocuments combined, and want to build a chart of them. I use a QWizard to ask for details for a specific chart from the user. QWizard is in a separate class as it can only be created like that. How can I pass the dynamic info based on user interaction back and forth between mainwindow class and QWizard class.

The only workaround that comes to my mind is to pass all the documents to the wizard class right at the beginning in its constructor, but then all the methods to filter info from the documents should be copied inside this class, too...

Pls, help! Thx!

BalaQT
28th March 2011, 07:02
How to send information between two classes where one class is instantiated inside the other?

(how would you get current number_of_apples in basket???????)
hi,
just pass the object in parameter,


class Apple
{
void Apple::methodinsideapple(Basket *b)
{
int total=b->number_of_apples;
}
}

and in Basket class method (cpp file),

instantiatedApple.methodinsideapple(this)

hope it helps
bala

viulskiez
28th March 2011, 07:04
How to send information between two classes where one class is instantiated inside the other?
You have to provide a method to access that class.
Example:


// basket.h
#include "apple.h"
class Basket
{
...
int number_of_apples;
Apple instantiatedApple;

public: // accessor
int numberOfApples() const
{ return number_of_apples; }
}

And below is a simple mechanism on how to access it.


// apple.h
class Basket;

class Apple
{
Basket* basket;
void methodinsideapple();

public:
inline void setBasket(Basket* basket)
{ this->basket = basket; }
}

// apple.cpp
#include "apple.h"
#include "basket.h"

Apple::methodinsideapple()
{
int numOfApples = basket->numberOfApples(); // you got it
...
}



How can I pass the dynamic info based on user interaction back and forth between mainwindow class and QWizard class.
You can use similar technique to solve your real problem. Maybe design pattern will be a great solution for complex class interaction.

squidge
28th March 2011, 07:47
I would recommend viulskiez's approach over balas. The former provides useful encapsulation over the data which the later does not.

falconium
28th March 2011, 16:29
Ahh, I see... so basically, the easiest would be to pass the pointer variable to the constructor of the instantiated class.
Yesterday I was trying to find a solution, and came to the idea of slots/signals, but that is much more complex to maintain properly.
Thank you!