// "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
}
Passing objects between functions
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.
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).
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).
Like he said
you should make the objects privates attributes.
This way you will be able to use the objects everywhere in the class functions. Should save you lot's of trouble
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
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.
you can pass an object by reference or by pointer.
internally in your foo class, it makes a lot more sense to do this:
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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement