开发者

Create reference to new object

I am just learning C++, and I've come across the following conundrum:

As a C++ newbie, I've read that using reference instead of pointers (when possible) is generally a good idea, so I'm trying to get into the habit early. As a result, I have a lot of methods which have the general form of

void myMethod(ParamClass const& param);

Now, I'm wondering what is the best way to call these methods. Of course, each call will need a different object passed as a parameter, and as far as I know the only way to create it is the new operator, so now I'm doing the following:

myObject.myMethod(*new ParamClass(...));

Whi开发者_如何学运维le this method totally works, I'm wondering if there isn't another already-established "c++ way" of doing this.

Thanks for the help! Dan


You should try not to use new, to begin with, as using it brings the trouble of memory management.

For your example, just do the following:

int main(int, char*[])
{
  SomeObject myObject;

  // two phases
  ParamClass foo(...);
  myObject.myMethod(foo);

  // one phase
  myObject.myMethod(ParamClass(...));

  return 0;
}

I recommend the first method (in two times) because there are subtle gotchas with the second.

EDIT: comments are not really appropriate to describe the gotchas I was referring to.

As @Fred Nurk cited, the standard says a few things about the lifetime of temporaries:

[class.temporary]

(3) Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception. The value computations and side effects of destroying a temporary object are associated only with the full-expression, not with any specific subexpression.

(5) The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference [note: except in a number of cases...]

(5) [such as...] A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.

This can lead to two subtle bugs, that most compilers do not catch:

Type const& bound_bug()
{
  Type const& t = Type(); // binds Type() to t, lifetime extended to that of t
  return t;
} // t is destroyed, we've returned a reference to an object that does not exist

Type const& forwarder(Type const& t) { return t; }

void full_expression_bug()
{
  T const& screwed = forwarder(T()); // T() lifetime ends with `;`
  screwed.method(); // we are using a reference to ????
}

Argyrios patched up Clang at my request so that it detects the first case (and a few more actually that I had not initially thought of). However the second can be very difficult to evaluate if the implementation of forwarder is not inline.


Try: myObject.myMethod(ParamClass(...)); in C++, unlike Java, you do not always need to say new to create a new object.


The established way of doing it is with an automatic local variable:

ParamClass myParam;
myOjbect.myMethod(myParam);

By using new in the way you did, you're generating a memory leak. Nothing will dispose of that object once the function returns - C++ does not have garbage collection as some other languages do.


You need to be aware of the lifetime of the object. If you pass *new ParamClass to a function, you're giving ownership of the new object to the function. If the function does not destroy it (and it should never do that given a reference), you'll get a memory leak.

Instead, you should do something like this:

ParamClass myParamClass(...);
myObject.myMethod(myParamClass);


When you write

myObject.myMethod(*new ParamClass(...)); 

you lose the pointer to the new'd object. That is, this will work, but you won't be able to later delete the object. So you can do this:

ParamClass pc(...);
myObject.myMethod(pc);

or, easier

myObject.myMethod(ParamClass(...));

or, if dynamic allocation is necessary for some inexplicable reason

ParamClass* pPc = new ParamClass(...);
myObject.myMethod(*pPc);
...
delete pPc;

or, use smart pointers to avoid manual deletion. Something like:

boost::scoped_ptr<ParamClass> spPc(new ParamClass(...));
myObject.myMethod(*pPc);

Hope this helps


Just note that there is big difference between assigning the value of an object (I mean the object of the user defined class) previously created to new object in java and c++ , and it is about :

1- in C++ : object new =(object) older [ create a copy of the object older to newer and when you modify newer ,the older will not change !]

2- in java : object new =(object) older [create a reference to the older object and when you modify the newer , the older also will change (very very important)]

conclusion :

in java : "object new =(object) older" is the same as "object &new =(object) older" in c++.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜