Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is stack-only variable possible in C++?

Tags:

c++

stack

Currently I want to make wrapper accessor class in multithreaded environment. The purpose of the class is simple - Grab a lock on its construction and release a lock on its destruction. Other than that, it's identical to a normal pointer. Moreover, I want to prevent it from being created on heap area to ensure that the lock will be released eventually.

The inverse is fairly easy (private constructor with factory method), but I don't know that a stack-only variable is possible. Is there anyway?

like image 946
summerlight Avatar asked Oct 26 '10 09:10

summerlight


1 Answers

Well, what about you overload operator new for your class and delcare it private?

like image 105
Armen Tsirunyan Avatar answered Oct 09 '22 23:10

Armen Tsirunyan