Jump to content
  • Advertisement
Sign in to follow this  
Just Chris

Passing objects between functions

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble passing objects between member functions, since these objects are not declared in the scope of the main function. Rather, they are declared in one function and used in a different function, both from the same class. I know that the use of pointers and references comes into play here, but don't know where to use them. And if I continue working on this it will just create more problems in the long run, so I'd like to solve this one soon.
// "Person" and "Conflict" classes are declared first

void foo::start(){
  Person Hero;
  Person SomeOtherGuy; // making objects from person class
  // functions from Person are used here
}

void foo::middle(){
  Conflict mainEvent; // object from conflict class
  mainEvent.startConflict(Hero, SomeOtherGuy); //Here's where I'm having the problem
}


Share this post


Link to post
Share on other sites
Advertisement
If you want to use an object in multiple functions of the same class you need to either declare the object in the class scope (versus a member function scope as youre doing now) - or call the middle() function from the start() function, and pass a reference or pointer to the object as an argument.

so, in other words, either declare SomeOtherGuy as a public, private, or protected member of foo, or call middle() from start() (probably not a viable option). There may be other ways if the functions/class are static, but I'm not sure about that, and it probably wouldn't be ideal anyways.

The problem is that local variables (like the two person's youre declaring in start()) are destroyed as soon as you leave the parent function, so they don't even exist to be used in the middle() function.

It's the same reason why you can't access "int i" which you made in your main function from inside your class functions. Well, almost the same... (there int i still exists until the program stops, here person SomeOtherGuy is destroyed as soon as start() ends).

Share this post


Link to post
Share on other sites
Like he said

you should make the objects privates attributes.


class CTraitement
{
private:
Person Hero;
...





This way you will be able to use the objects everywhere in the class functions. Should save you lot's of trouble

Share this post


Link to post
Share on other sites
Looks like that suits my needs for one class, but now I realized that I want to use these objects in more than one class. Like in my example above, use the same Person objects in class "foo" and another new class "bar". But if I declare those objects in more than one class, wouldn't that create duplicates, or more likely cause conflicting types? The Person class isn't derived from any other classes, by the way.

Share this post


Link to post
Share on other sites
you can pass an object by reference or by pointer.



void Conflict::startConflict(Person& Protagonist, Person& Antagonist)
{
-- can now use these as normal
Antagonist.dieHorriblyFrom(Protagonist);
}

.....

void foo::middle(){
Conflict mainEvent; // object from conflict class
mainEvent.startConflict(Hero, SomeOtherGuy);
}





internally in your foo class, it makes a lot more sense to do this:

class Foo
{
public:
void start();
void middle();
private:
Person Hero, SomeOtherGuy;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!