关于堆对象和C ++&传递参考参数 [英] About ownership of heap objects and C++ & pass-by-reference parameters

查看:192
本文介绍了关于堆对象和C ++&传递参考参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的班级是:

  class NumberedString:public Object {
public:
String newName;
short nameID;
NumberedString(String& newName,short nameID):newName(newName),nameID(nameID){}
};

HashMap uniqueStrs; // For later。

此实例化将传递给 HashMap 它接管其堆分配的所有权:



在HashMap.h中:

  virtual result Add(const Object& key,const Object& value); 

现在这是我困惑的地方。我在添加的行中分配 String

  uniqueStrs.Add(*(new String(LXYX_say)),* pNewLoc); 

然后 HashMap 我虽然只接受参考它。也许我失去了十年到 C 在新的千年,但我认为这是不可能的



如果不是我应该能够写如下:

 〜NumberedString(){
delete& newName;
}

我的类,但我从来没有猜到,除非我看到这个库 HashMap :: RemoveAll ) 执行相同操作。 此问题说明这是不可能的,但会回到依赖 auto_ptr shared_ptr 但我的平台只支持STL(标准模板库(http://www.sgi.com/tech/stl/))。 (出整个标准C ++库)。



由评论提示的链接

/ strong>



我无法将链接发布为评论,因此请参阅添加建议使用: here And Benj,字符串不是std ::



我知道这可能会导致崩溃的尝试删除堆栈对象,但我不知道 HashMap 可以声称能够删除堆对象。



为了回应无用的

strong>



@Useless:可能不能传递到 foo(int& bar)变量 * pBar ,声明 int pBar = new int(1); 然后 foo 假设拥有

  foo(int& bar){
int * __ pBar =& ;酒吧;
delete __pBar;
}

?我会尝试,但我开始谨慎不要相信文件说的太多。虽然它是从

 的类别的头部生成

class _EXPORT_BASE_ HashMap:
public IMap,
public Object
{
virtual result Add(const Object& key,const Object& value);
//其他东西
};


解决方案

delete的唯一
语法规则是它的操作数必须是一个指针。
语义:指针必须是从 new 返回的值,这就是
这个idiom stinks;如果我看到一个函数接受一个const引用,
我通常是假定我可以传递一个局部变量,
或临时,或者这样。在这种情况下, delete 会导致
真的很大的问题。



更广泛地说,看过库文档:我会
避免这个库像瘟疫。它让我想起了一个来自
NHS的库,它在C ++的早期很普遍:它需要
一切都源自 Object ,容器包含 Object *
当时的经验(1980年代末)导致
的结论,它没有工作,并且是
添加模板的语言的动机的一部分,所以我们可以写
工作的东西。使用这个库基本上可以追溯到25年的时间,
抛出我们从那以后学到的一切。 (Java遵循
类似的路由大约10年后,所以它不是特定于
C ++的基本上,提出的解决方案是为
语言开发的完全动态类型检查,如Lisp,Smalltalk或更多
最近Python,并且不工作在静态类型
检查语言,如C ++或Java。)


I would like my class to be:

class NumberedString : public Object {
public:
    String newName;
    short nameID;
    NumberedString(String &newName, short nameID) : newName(newName), nameID(nameID) {}
};

HashMap uniqueStrs;//For later.

An instantiation of this will be passed to a HashMap which takes over ownership of its the heap allocation:

In HashMap.h (say):

virtual result Add(const Object& key, const Object& value);

Now this is where I get confused. I was allocating the String in the line that called Add:

uniqueStrs.Add(*(new String(L"XYX_say")), *pNewLoc);

The HashMap would then free this memory for me despite only accepting a reference to it. Maybe I lost a decade to C over the new millenium but I thought this was impossible?

If it's not then I should be able to write something like:

~NumberedString() {
    delete &newName;
}

for my class, but I'd never have guessed unless I saw this library HashMap::RemoveAll() doing the equivalent. This question states that this is impossible but falls back to reliance on auto_ptr and shared_ptr but my "platform supports only STL(Standard Template Library (http://www.sgi.com/tech/stl/))." (out of the entire "Standard C++ Library"). Could all answers please refrain from such references.

Thank you.

LINKS prompted by comments

I can't post the links as comments so please see the Add method and an example of its suggested use: here And Benj, String is not std::string no, sorry.

ALSO

I know it can cause crashes trying to delete stack objects but I don't get how HashMap can claim to be able to delete heap objects. I have coded up the above class to try and recreate this behaviour but I cannot accomplish the feat, hence the question.

In response to "Useless"

@Useless: Mightn't it be possible to pass to foo(int &bar) the variable *pBar, declared int pBar = new int(1); and then foo assumes ownership with

foo(int &bar) {
    int *__pBar = &bar;
    delete __pBar;
}

? I was going to try, but I am beginning to be cautious not to believe too much of what the documentation says. Though it was generated from the header which is saying

class _EXPORT_BASE_ HashMap :
    public IMap,
    public Object
    {
    virtual result Add(const Object& key, const Object& value);
        //other stuff
    };

解决方案

Well, there's certainly nothing syntactically wrong with it. The only syntax rule for delete is that its operand has to be a pointer. Semantically: the pointer must be value returned from new, and that's where this idiom stinks; if I see a function taking a const reference, I'm normally justified in supposing that I can pass it a local variable, or a temporary, or such. In which case, the delete is going to cause really big problems.

More generally, having looked at the library documentation: I would avoid this library like the plague. It reminds me of a library from the NHS, which was widespread in the early days of C++: it requires that everything derive from Object, and containers contain Object*. Experience with this library back then (late 1980's) led to the conclusion that it didn't work, and were part of the motivation for adding templates to the language, so that we could write things that did work. Using this library is basically going back 25 years in time, and throwing out everything we've learned since then. (Java followed a similar route about 10 years later, so it's not something specific to C++. Basically, the solution proposed is one that was developed for languages with full dynamic type checking, like Lisp, Smalltalk or more recently Python, and doesn't work in languages with static type checking, like C++ or Java.)

这篇关于关于堆对象和C ++&传递参考参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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