• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How common is this C++ pattern?

 
Henry Wong
author
Marshal
Pie
Posts: 21435
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Author
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21435
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic