This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes C / C++ and the fly likes How common is this C++ pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "How common is this C++ pattern?" Watch "How common is this C++ pattern?" New topic
Author

How common is this C++ pattern?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40


Since we have a C++ expert here this week, I am going to take advantage of it...


I only use C++ for about 20% of the time, so bear with me. But while maintaining code, which has been developed by others, I found a coding pattern which seems... weird.

The code pattern looks like this...



At the top of a code block is a variable declaration -- and instantiation. The variable isn't directly used in the block at all -- and about the only communication with the variable is the parameters when calling the constructor.

Basically, the constructor of the variable will allocated resources, and the destructor will clean up after it, when it leaves the block (goes out of scope). It looks quite elegant, but the first time I saw this, it took me quite a while to figure out the purpose.

Is this pattern common?

Thanks,
Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prentiss Knowlton
Author
Greenhorn

Joined: Dec 18, 2009
Posts: 7
Hi, Henry --

The pattern you've shown instantiates a value object, common in native C++, and would be allocated on the stack.

The C++ 2008 .NET coding of this pattern would likely be instead to instantiate a reference object, to wit:

Then, instead of using var.element notation in the use of this object, you would use var->element notation instead.

Since, per your example, no explicit use is made of the var reference object, the destructor is automatically invoked (by the managed code garbage collector) when the object is no longer needed.

At your service,
Pren


Henry Wong wrote:
Since we have a C++ expert here this week, I am going to take advantage of it...


I only use C++ for about 20% of the time, so bear with me. But while maintaining code, which has been developed by others, I found a coding pattern which seems... weird.

The code pattern looks like this...



At the top of a code block is a variable declaration -- and instantiation. The variable isn't directly used in the block at all -- and about the only communication with the variable is the parameters when calling the constructor.

Basically, the constructor of the variable will allocated resources, and the destructor will clean up after it, when it leaves the block (goes out of scope). It looks quite elegant, but the first time I saw this, it took me quite a while to figure out the purpose.

Is this pattern common?

Thanks,
Henry


Prentiss Knowlton

People don't care how much you know until they know how much you care -- Cavett Robert
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40

Prentiss Knowlton wrote:
Since, per your example, no explicit use is made of the var reference object, the destructor is automatically invoked (by the managed code garbage collector) when the object is no longer needed.


That's the interesting part about this pattern. The object is for an important resource that can't wait for a gc. The destructor must be called, and at the instant that the object goes out of scope. For example, it grabs a mutex that can't be held very long.

Personally, I just found the pattern weird. Mainly because this is something that I don't see in Java -- and is actually kinda elegant... ... It is a way to have a pieces of code execute; at the start of a code block, and at the end of the code block.

Henry
 
Don't get me started about those stupid light bulbs.
 
subject: How common is this C++ pattern?