Advertisement

Function returning a pointer

Started by September 26, 2002 12:13 PM
4 comments, last by algumacoisaqualquer 22 years, 1 month ago
Hello... I am trying to build an array of pointers to classes, and I want to make a function where I will get the last member of the array. Since that member is an pointer to my Object class, the function has to return a pointer. How do I do that. Also... My object class has 3 derived classes, is there a way I can create an object array, and store the 3 diferent classes in this same array, or do I have to create 3 separate arrays??
Just return a pointer to the type of object.

OBJECT* function()

Advertisement
Just return a pointer to the type of object.

OBJECT* function()

OBJECT *my_object;
.
.
.
return my_object;



Hey it worked, thanks a lot!
quote: Original post by algumacoisaqualquer
Also... My object class has 3 derived classes, is there a way I can create an object array, and store the 3 diferent classes in this same array, or do I have to create 3 separate arrays??


Yes you can store objects of any classes dirived from the same base class in the same array.

The difference between us and a computer is that, the computer is blindingly stupid, but it is capable of being stupid many, many million times a second.
-Douglas Adams
The difference between us and a computer is that, the computer is blindingly stupid, but it is capable of being stupid many, many million times a second.-Douglas Adams
quote: Original post by Slartibartfastm
Yes you can store objects of any classes dirived from the same base class in the same array.

True, but doing so is downright dangerous. Better to use pointers to the objects as the array element and to ensure the base class has a virtual destructor.

This topic is closed to new replies.

Advertisement