In Windows for C ++, I have some Object Factories that make an object a pointer function to an object And object to back up a built object.
Zero CreateInfoObject (AbstractInfo ** info); // Subscription Function
Abstract Info is a base class, in which we have many types of information objects.
I thought that now I can create an info object:
MyInfoObject * InfoObj = NULL; Information obtained from the ObjectEfO Item InfoFatari FC; Fc.CreateInfoObject (& InfoObj); // Now I have to return my initial indicator
but it says that this artist can not do ... what's wrong?
Error: MyInfoObject ** _ Abstract Info ** from W64 ** Edit: The first answer indicates that the interface is dangerous, can not see who is allocating etc. .. How can I improve?
We think of the possible implementation of CreateInfoObject
:
Zero InfoFactory :: CreateInfoObject (Abstract Info ** Info) {* info = new SuperInfo; }
Now, do not have the same rights in SuperInfo
and MyInfoObject
?
This is the reason, in general, the following is forbidden:
struct base {}; Structure D1: base {}; Structure D2: base {}; Int main (int argc, char * argv []) {base ** base = nullptr; D1 * D = Nalper; Base = D; }
Because it will allow D1
to point to something unrelated.
There are several solutions:
// 1. Simple abyserfonie * info = nullptr; Fc.CreateInfoObject (info); // 2. Better interface std :: unique_ptr & lt; AbstractInfo & gt; Info = fc.CreateInfoObject ();
Then, if you certainly know that you can actually use a MyInfoObject
:
< Code> MyInfoObject * MyInfo = static_cast & lt; MyInfoObject * & gt; (Information);
Or if you are unsure:
MyInfoObject * myInfo = dynamic_cast & lt; MyInfoObject * & gt; (Information);
will be set to
These derivatives).myInfo
withnullptr
if everinfo
hasinfo> MyInfoObject However, keep in mind that your interface is really fierce. It is a very similar eye-eye and it is not clear if the memory has actually been allocated or not ... and if it is Who is responsible for assuming
Edit :
Good In C ++ style, we both represent ownership and ensure cleanliness For RAII use. RAI is well known, although not very signal, I prefer the new SBRM (Scope Bound Resources Management) itself.
The idea is that instead of using a bare indicator, which is owned (i.e. you have to remove it?) You should use the smart pointers , such as < Code> unique_ptr
.You can also use it to avoid the two-step initialization process, the return parameter of the method (first create the pointer, then point to the object) Here is a brief example:
< Pre>typedef std :: unique_ptr & lt; AbstractInfo & gt; AbstractInfoPtr; // Note: If you know that it returns a MyInfoObject / returns then you can use std :: unique_ptr & lt; MyInfoObject & gt; SarfenfoFort Infographics :: CreateInfo Object () {Return Abstract Infobox (New MyInfo Object); } // Usage: int main (int argc, char * argv []) {InfoFactory factory; Abstract Infotrale Info = Factory. Crete Infoobject (); / /> Do something / // information goes out of the scope, `` delete call at its point
Here, there is no ambiguity in respect to ownership.
Also, note how you think your question is better here:
std :: unique_ptr & lt; MyInfoObject & gt; Info = factory.CreateInfoObject ();
will not be compiled because you have not converted AbstractInfo *
to MyInfoObject *
without using static_cast
Or dynamic_costs
Comments
Post a Comment