Basically I'm having difficulty figuring out what to return to a function. Here is my problem:

I have a class called Zoo that contains Animal objects in an array of pointers called cages e.g..

class Animal{
  double weight;
  double height;
  double speed;
  double power;
  Animal(){}
};

class Zoo{
  Animal ** cages;

  Zoo(){
    cages = new Animal*[10];
  }
};

Each animal object has some properties like height,weight,power,speed, etc..

The problem is: What is the best way to return the data contained by cages to a function that needs to NOT know about the zoo class. So, I cannot simply return a pointer to the zoo class, or an object of the zoo class, or an object of the animal class. So what in the world can I return? My initial thought it to make an array of strings such that each element contains the data from a cage seperated by commas or something, but I HATE parsing strings. Is there perhaps a better way????

Recommended Answers

All 5 Replies

Why doesn't the recieving/calling class know about the Zoo class?

That is just the problem specification. This is a contrived problem, not a realistic one I think. I think if I were really to be designing something like this, then the calling function WOULD know about the zoo class, but the problem that I have clearly states that I cannot return the zoo or the animal class, I have to return something else. But, the question is what. Any thoughts?

I'd need to know what the calling function/receiving funtion was going to do with the information and what information the calling function passed to the called function. My first thought would be to pass the information to the called function by reference and have the return value of the called function be type void, but that may not be what is intended.

I apologize for the lack of information. The problem just has me make a function, that calls a function to create the zoo, add animals to the zoo, and then I need to return to the original function all the data in the zoo cages, but I cannot return a zoo or an animal object. Back in the original function, I simply print all the data out.

<returntype?> makeZoo(){
   Zoo z;
   z.makeAnimals();  //this function loops on user input and fills the zoo with animals
  something = z.convertData();
  return something;
}

int someFunction(){
   someDataObject = makeZoo();
  //print all the animal data that someDataObject contains
}

so the problem remains, what in the world do I return to someFunction to print out the zoo when I can't return any zoo/animal pointers or objects? The more I talk to you about this problem, the stupider it seems to me. Perhaps the point is to show that this is stupid and annoying, lol. I dunno. I think I'm just going to go with my original idea and return an array of strings containing the data and parse it. I just hate parsing but I cannot come up with anything else here given the constraints.

Thankyou for your replies, it was helpful just talking about the problem.

>>Perhaps the point is to show that this is stupid and annoying, lol

May not be far off the mark.

The way I see it at the moment, if the only thing you have control over in the scenario as posted in #5 is the return type, that is you can't send an array and and int to makeZoo(), then creating creating and returning an array (vector) in makeZoo() makes some sense, though you'll need to return a pointer to type, not the array itself. That's because you can't assign one array (vector) to another by doing this:

someDataObject = makeZoo();

If the return type is pointer to type then you can assign one pointer to another. If the array is declared with dynamic memory, then the memory the array uses will remain valid until you delete them someplace within the program. Since the name of the array acts as a pointer to type you can return that and I think it will work. The problem would be how will you know when to stop when printing the results back in someFunction()? Just returning a pointer won't tell you how many objects are in the array so you run a serious risk of over reading the array you've returned.

To deal with that you could create a new type (either a struct or a class) which would contain both the array and the number of items in the array and return an object of that type so you'd know when to stop and not read past the end of the array returned.

Alternatively, you could return a container that doesn't need to know how many items are in it to know when to stop printing when you get back to the calling function. The prototype that comes to mind is a list. You can pass the head pointer back to the calling function and print until you reach the tail node without needing to know how many items are actually in the list.

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.