managing objects derived from the same base class using a container
-
Yes, except for: 1. Why are you using an array size reference in your instantiations? All you should need is:
derivedclass * Derived1 = new derivedclass();
2. Your container is defined to use
somebaseclass *
types, so your calls topush_back
should be:AllObject->push_back((somebaseclass*)Derived1);
or better still, using proper C++ casts:
AllObject->push_back(reinterpret_cast(Derived1));
thanks, that helps.
-
Yes. If objects belong to different classes, the only way to store them in a container is to use pointers, because the container allocates the same amount of memory for each entry. If you also want the container to delete an entry when you erase it, declare it as, for example,
vector
>.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Quote:
because the container allocates the same amount of memory for each entry
that is an interesting fact
-
thanks, that helps.
You are welcome. I also think @Mircea-Neacsu's advice about
unique_ptr
is well worth taking. -
You are welcome. I also think @Mircea-Neacsu's advice about
unique_ptr
is well worth taking.pointers from a library, that`s a topic that`s a bit too advanced or complicated for my present day understanding.
-
pointers from a library, that`s a topic that`s a bit too advanced or complicated for my present day understanding.
If you can handle raw pointers, then C++ smart pointers should be easy to understand. Google for C++ unique_pointer tutorial and read through a few of the returned hits. It's fairly straight forward, and in general new C++ development should use the smart pointers instead of using raw (e.g. new/delete).
Keep Calm and Carry On
-
pointers from a library, that`s a topic that`s a bit too advanced or complicated for my present day understanding.
-
If you can handle raw pointers, then C++ smart pointers should be easy to understand. Google for C++ unique_pointer tutorial and read through a few of the returned hits. It's fairly straight forward, and in general new C++ development should use the smart pointers instead of using raw (e.g. new/delete).
Keep Calm and Carry On
The problem is I hate complicated syntax. Containers are already complicated syntax for me, combine that with another object (pointer) from a library and it becomes unintelligible mess. I will use a complicated feature when I really need to use it and there is no other way around it. Usually I need to use a feature a couple months before I can move on to something more complicated.
-
Richard you really think so?
-
Richard you really think so?
-
ok, it was a joke
-
Yes, except for: 1. Why are you using an array size reference in your instantiations? All you should need is:
derivedclass * Derived1 = new derivedclass();
2. Your container is defined to use
somebaseclass *
types, so your calls topush_back
should be:AllObject->push_back((somebaseclass*)Derived1);
or better still, using proper C++ casts:
AllObject->push_back(reinterpret_cast(Derived1));
I guess converting the object(pointer) back to it`s original form when time comes to use it somewhere works the same
derivedclass * DerClpointer1 = (derivedclass *)AllObjects->at(0);
Is there a way to check is the conversion is valid? like if an object is of a certain type. For instance how do I convert all objects to their derived state type in a for loop?
for(int i =0; i< AllObjects->size();i++ )
{
// if AllObjects->at(i) is of type derivedclass covert to derivedclass
} -
I guess converting the object(pointer) back to it`s original form when time comes to use it somewhere works the same
derivedclass * DerClpointer1 = (derivedclass *)AllObjects->at(0);
Is there a way to check is the conversion is valid? like if an object is of a certain type. For instance how do I convert all objects to their derived state type in a for loop?
for(int i =0; i< AllObjects->size();i++ )
{
// if AllObjects->at(i) is of type derivedclass covert to derivedclass
}