将参数指针返回到对象 [英] Returning an argument pointer to an object

查看:127
本文介绍了将参数指针返回到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ for Windows中,我有一些对象工厂,通过传递一个指向该对象的指针来创建一个Info对象,并返回一个创建的对象。

  void CreateInfoObject(AbstractInfo ** info); //创建函数

AbstractInfo是一个基类,我们有很多类型的Info对象派生。



我想我现在可以创建一个Info对象,如下所示:

  MyInfoObject * InfoObj = NULL; //派生自AbstractInfo对象
InfoFactory fc;

fc.CreateInfoObject(& InfoObj); //现在我想让我的初始化指针回来

但它说它不能执行转换。 哪里不对?



错误:
无法从MyInfoObject转换** _ W64到AbstractInfo **



第一个答案提到接口是可怕的,看不到谁分配等...我如何改进?

解决方案

一个可能的实现 CreateInfoObject

  void InfoFactory :: CreateInfoObject ** info)
{
* info = new SuperInfo;
}

现在, SuperInfo MyInfoObject 没有什么共同点?



这是为什么,一般来说,

  struct Base {}; 
struct D1:Base {};
struct D2:Base {};

int main(int argc,char * argv [])
{
Base ** base = nullptr;
D1 * d = nullptr;
base = d;
}

因为它允许 D1

 <$ c $> 

c> // 1. Simple
AbstractInfo * info = nullptr;
fc.CreateInfoObject(info);

// 2.更好的接口
std :: unique_ptr< AbstractInfo> info = fc.CreateInfoObject();

然后,如果你确定知道你有一个 MyInfoObject 您可以使用:

  MyInfoObject * myInfo = static_cast< MyInfoObject *> 

或如果您不确定:

  MyInfoObject * myInfo = dynamic_cast< MyInfoObject *>(info); 

这将设置 myInfo nullptr 如果 info 没有指向 MyInfoObject 或派生)。



请记住,你的界面是可怕的。它非常C-ish,目前还不清楚是否实际分配了内存...如果是,则负责处理它。



EDIT



好的 C ++风格中,我们使用RAII表示所有权并确保清理。 RAII是众所周知的,虽然不是很有指示性,我本人更喜欢新的SBRM(范围绑定资源管理)。



这个想法是,而不是使用裸指针(例如,您必须调用delete)吗?您应该使用智能指针,例如 unique_ptr



您还可以使用方法的返回参数,以避免进行两步初始化过程(首先创建指针,然后使其指向一个对象)。这里是一个简明的例子:

  typedef std :: unique_ptr< AbstractInfo>摘要: 

//注意:如果你知道它返回一个MyInfoObject
//你也可以返回std :: unique_ptr< MyInfoObject>
AbstractInfoPtr InfoFactory :: CreateInfoObject()
{
return AbstractInfoPtr(new MyInfoObject());
}

//用法:
int main(int argc,char * argv [])
{
InfoFactory factory;
AbstractInfoPtr info = factory.CreateInfoObject();

//做某事

} //信息超出范围,在它的引脚上调用`delete`

这里没有关于所有权的歧义。



此外,请注意如何更好地了解问题:

  std :: unique_ptr< MyInfoObject> info = factory.CreateInfoObject(); 

无法编译,因为您无法转换 AbstractInfo * static_cast dynamic_cast 到<$ c $> 。


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?

解决方案

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.

这篇关于将参数指针返回到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆