问题
In C++ for Windows, I have some object factory that is supposed to create a series of Info object by passing a pointer to the object to a Create function and returning a created object.
void CreateInfoObject(AbstractInfo** info); // The creation function
AbstractInfo is a base class of which we have many types of Info objects derive.
I thought I could now create an Info object as follows:
MyInfoObject* InfoObj = NULL; // derived from AbstractInfo object
InfoFactory fc;
fc.CreateInfoObject(&InfoObj); // Now I want to get my initialized pointer back
But it says it cannot do the cast... What is wrong?
ERROR: Cannot cast from MyInfoObject**_W64 to AbstractInfo**
EDIT: The first answer mentions that the interface is horrid, cannot see who's allocating etc... How can I improve?
回答1:
Let's think about a possible implementation of CreateInfoObject
:
void InfoFactory::CreateInfoObject(AbstractInfo** info)
{
*info = new SuperInfo;
}
Now, SuperInfo
and MyInfoObject
do not have anything in common right ?
This is why, in general, the following is forbidden:
struct Base {};
struct D1: Base {};
struct D2: Base {};
int main(int argc, char* argv[])
{
Base** base = nullptr;
D1* d = nullptr;
base = d;
}
As it would allow D1
to point to something unrelated.
There are several solutions:
// 1. Simple
AbstractInfo* info = nullptr;
fc.CreateInfoObject(info);
// 2. Better interface
std::unique_ptr<AbstractInfo> info = fc.CreateInfoObject();
Then, if you know with certainty that you have, in fact, a MyInfoObject
you can use:
MyInfoObject* myInfo = static_cast<MyInfoObject*>(info);
or if you are unsure:
MyInfoObject* myInfo = dynamic_cast<MyInfoObject*>(info);
which will set myInfo
to nullptr
if ever the info
did not pointed to an instance of MyInfoObject
(or derived).
Bear in mind though, that your interface is really horrid. It very C-ish and it is unclear whether memory is actually allocated or not... and who is responsible for handling it if it is.
EDIT:
In good C++ style, we use RAII to both denote ownership and ensure clean-up. RAII is well-known though not very indicative, I myself prefer the newish SBRM (Scope Bound Resources Management).
The idea is that instead of using a bare pointer, which does not indicate anything about ownership (ie do you have to call delete on it ?) you should use a smart pointer, like for example unique_ptr
.
You can also make use of the return parameter of the method, to avoid having a two-steps initialization process (first create the pointer, then make it point to an object). Here is a concise example:
typedef std::unique_ptr<AbstractInfo> AbstractInfoPtr;
// Note: if you know it returns a MyInfoObject
// you might as well return std::unique_ptr<MyInfoObject>
AbstractInfoPtr InfoFactory::CreateInfoObject()
{
return AbstractInfoPtr(new MyInfoObject());
}
// Usage:
int main(int argc, char* argv[])
{
InfoFactory factory;
AbstractInfoPtr info = factory.CreateInfoObject();
// do something
} // info goes out of scope, calling `delete` on its pointee
Here, there is no ambiguity in regard to the ownership.
Also, note how you better understand your question here:
std::unique_ptr<MyInfoObject> info = factory.CreateInfoObject();
would not compile because you cannot convert a AbstractInfo*
to a MyInfoObject*
without using static_cast
or dynamic_cast
.
回答2:
Because CreateInfoObject()
takes a pointer-to-a-pointer-to-an AbstractInfo
, it's possible for the function to return an instance of AbstractInfo
that is not an instance of MyInfoObject
. So you could end up with a pointer to MyInfoObject
that actually points to a DifferentInfoObject
.
Change the MyInfoObject *InfoObj
to AbstractInfo *InfoObj
and it should work. Don't cast away the conversion with anything but dynamic_cast<>
because you don't know for certain that CreateInfoObject()
returns an instance of that subclass.
回答3:
The compiler told you what's wrong. You cannot convert a pointer of type T* to a pointer of type U* when T and U have nothing to do with each other. Here, T=MyInfoObject*, U=AbstractInfo* and these are two distinct pointer types that don't share any inheritance relationship.
回答4:
A pointer to a pointer just isn't as flexible as a pointer to an object. The compiler will strictly enforce the type without regard to the inheritance tree.
The safest way to fix this code is with a double assignment:
MyInfoObject* InfoObj = NULL; // derived from AbstractInfo object
AbstractInfo* temp = NULL;
InfoFactory fc;
fc.CreateInfoObject(&temp);
InfoObj = dynamic_cast<MyInfoObject*>(temp);
回答5:
Consider that happens in CreateInfoObject
.
Lets say there is another subclass of AbstractInfo
, call Foo
.
Inside CreateInfoObject
we create a new Foo
and assign it to *info
. (Allowed upcast).
But outside we now have Foo
inside a MyInfoObject**
which is wrong.
来源:https://stackoverflow.com/questions/3805611/returning-an-argument-pointer-to-an-object