One Object for two functions

Hi, I need to know if it is possible/not possible to share one object for two functions..

o.o
Last edited on
If you mean to use one object to call two different functions? Yes.

If you mean to pass on your object from function to function. Also yes.
It is possible.
lets say if i have...

1
2
3
4
5
6
7
8
9
10
class House
{
public:
int detail();
int sample();
};
int House::detail()
{....}
int House::sample()
{....}


then i want to make an object "House object1;"
how do I share it with two functions? D:
Last edited on
What do you mean by "share"?

If you create a House you can call both House::detail() and House::sample(), but not at the same time.
1
2
3
House object1;    // Create House object
object1.detail(); // Call House::detail()
object1.sample(); // Call House::sample() 
By share, I think he means call for a function, and send in the object as parameter.

If you create House Object1;

You can just do

1
2
3
4
5
object1.detail(object1)
int House::detail(House& object1)
{
  // Use your object here
}
That looks like the opposite of the thread title: Two objects for one function
I dunno how to explain this but im gonna try...
this is my code that im trying to write(well just part of it)..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class House
{
  public:
     int write();
     int Display();
};

//this function writes information into a text file so im fstreaming

int House::write()
{ //then i have this object which is an array..
     House available[100];
....
....
}

//the question is how can i use/access (or whatever the word is) the object available[100]
//inside the next function which is the int Display()..
int House::Display()
{
......
}


I hope you got my point D:
Last edited on
@peter87. The title was One Object for two functions

Which is pretty much what my code showed :D

@tomitones.

In your write function, you do this

1
2
3
4
5
6
7
int House::write()
{ 

House available[100];
	
	Display(available);
}


And now you can access it in display

1
2
3
4
5
int House::Display(House* obj1)
{
	
	obj1[0].something
}
Last edited on
@TarikNeaj thanks for the reply..

ummm newb question, what is that ".something"? what should I replace there? :D
and if you say
int House::Display(House* obj1)
then i have to place a parameter in my function display in my class? D:
yes. You'd have to place the parameters in your class so it looks the same.

The do something I just put there. Since obj1 is an array. You can use obj1[0], obj1[1] etc, to access functions in your class.

for example:


obj1[0].write();

You can't access the available array from House::write() inside of House::Display() because available is a local variable. Instead, I think what you want to do is pass the available array to write() and Display:

1
2
3
4
5
6
7
8
9
10
House::write(House available[]);
House::Display(House available[]);
...
{
    House available[100];
    House someHouse;
    ...
    someHouse,write(available);
    someHouse.Display(available);
}

@TarikNeaj thank you for your reply...

I did manage to get the program working, thanks :D
I did what you said.

@dhayden thank you for you reply :D
Glad I could help :) And thanks for using code tags :DD
Topic archived. No new replies allowed.