Same function called twice, with different strings returned?

Hi! So I'm trying to call the say() function in my Fox class to print one string, and then when I call it again (for thing2), it prints something different. I'm not sure how I should go about getting two different values returned.

Here is a snippet of my code.

This is in the main function.
1
2
3
4
5
6
7
8
9
10
11
12
	Mammal* fox = MammalFactory(typeid(Fox));

	std::string thing1 = fox->say();
	std::string thing2 = fox->say();
	std::cout<<"What does the "<<fox->name()<<" say?\n";
	std::cout<<thing1<<"!\n";
	std::cout<<thing1<<"!\n";
	std::cout<<thing1<<"!\n";
	std::cout<<"What does the "<<fox->name()<<" say?\n";
	std::cout<<thing2<<"!\n";
	std::cout<<thing2<<"!\n";
	std::cout<<thing2<<"!\n";



This has the relevant parts from the source file that main is compiled with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mammal* MammalFactory(const type_info& ti){

           if(ti == typeid(Fox)){
		return new Fox();
           }
}


string Fox::say(){

	// return "WA PA PA PA PA PA POW!";
	// return "HATEE HATEE HATEE HO!";
	
}


^ So in say(), I'm having trouble figure out how to return out these two things separately when thing 1 is called and then when thing 2 is called, which thing 1 and thing 2 aren't defined in source file and only in main.
Last edited on
Pass the thing information to 'say' funtion, i.e like below code

1
2
3
4
5
6
7
string Fox::say(int i_thing)
{
	if(i_thing==1)
	  return "WA PA PA PA PA PA POW!";
	else if(i_thing==2)
	  return "HATEE HATEE HATEE HO!";
}


so, when you what to call thing1
std::string thing1 = fox->say(1);

similarly thing2
std::string thing2 = fox->say(2);
I would just use an array to store the values then do return array[i_thing]; //idk if those are supposed to be animal noises or what but I never heard those before
Topic archived. No new replies allowed.