One more question.
I'm working on a multi-file object,
There's no such thing. I assume you meant a multi-file /project/, with base
class in one module and derived in another? The file organization really
does not affect the mechanisms you're grappling with here. Whether
everything were in one big file or spread out across many, you'd still have
exactly the same issues.
so once I create a derived1 obj1 object
I need to store them in a "global container" to make them available even
after the call to the destructor function (that destroys the obj1 object).
Whoa. Not sure what you're trying to say there, but no object remains
"available" after its destructor has run; that's why it is called a
"destructor". From your code below it looks like you're defining an object
(obj1), making a "sliced copy" (a temporary base object created by "slicing
off" the derived part of obj1 and keeping what remains), putting a copy of
that base slice into your array, and then finally putting a pointer to that
base slice into your array of pointers. After the "creation function" code
runs, the only object that might be destroyed before your program completes
execution is going to be the original obj1 object (if execution reaches the
end of the block where obj1 is defined, and that isn't the end of main()).
The sliced copy you made will stick around "forever" (because the array is
at file scope), and so will the pointer you've installed in p_arr.
[BTW, did you know array indexing begins at 0, not 1? Just checking...]
I'm explaining all this so you know what your code is doing, but I doubt
you really want slices created in the first place...
It would be very useful for me to store these objects from different
subclasses (derived1, derived2 and so on) in an array but if I try to use
these code lines:
//global variable:
base arr[10];
base *p_arr[10];
//creation function:
derived1 obj1;
arr[1] = obj1;
p_arr[1] = &arr[1];
// now p_arr[1] points to a base object and not to a derived1 object.
so is there a way to store objects from different subclasses in a unique
"container", possibly with a random access (like arrays or vectors), without
loosing their original class properties?
Only by storing pointers, and the current thinking is that "smart pointers"
such as those available from the Boost library are the better choice for
storing in STL containers. The only kind of smart pointer provided by the
C++ Standard Library, std::auto_ptr, is specifically /not/ any good for
storing in containers due to its copying-means-transfer-of-ownership
semantics. Fortunately, libraries are now generally designed to not let you
store auto_ptrs in STL containers.
Thank you again for the help!
Marco
P.S. If these questions are too "basics" please let me know if there is a
more suitable group to post... I wouldn't like to be "annoying"! ;-)
I never consider anyone who takes the trouble to explain their questions to
the best of their ability (and disclose the level they're approaching the
questions from) to be annoying. If you'd prefer to post in a newsgroup
that was designed specifically for catering to "newbie"-level questions,
though, I'd invite you to go check out (man
I hate typing that out...)
-leor