473,721 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using const references as local smart pointer?

I am fine-tuning some last minute things in my c++ allocator lib and was
thinking of ways to manage local references to system objects created with
factory functions. I started to tinker around with using const references to
represent a smart-pointer template that uses intrusive reference counting.
Anyway, here is a short little program that should clearly demonstrate what
I am messing around with:

_______________ __________
#include <cstdio>
#include <exception>
template<typena me T>
class AutoRef {
mutable T *m_State;

AutoRef(T* const State) throw() : m_State(State) {
printf("(%p)Aut oRef::AutoRef(% p)\n", (void*)this, (void*)State);

~AutoRef() throw() {
T::RefCount_XAd d(m_State, -1);
printf("(%p)Aut oRef::~AutoRef\ n", (void*)this);

AutoRef(AutoRef const&);
AutoRef const& operator =(AutoRef const&);

void Cancel() const throw() {
m_State = 0;
printf("(%p)Aut oRef::Cancel()\ n", (void*)this);

T* operator ->() const {
if (! m_State) { throw std::exception( ); }
return m_State;

T& operator *() const {
if (! m_State) { throw std::exception( ); }
return *m_State;
class Foo {
typedef AutoRef<FooAuto Ref_t;

int m_RefCount;

typedef AutoRef_t const &Handle;

static AutoRef_t Create() {
Foo* const This = new Foo;
printf("(%p)Foo ::Create()\n", (void*)This);
return AutoRef_t(This) ;

static void Destroy(Foo* const This) {
delete This;
printf("(%p)Foo ::Destroy()\n", (void*)This);

Foo() : m_RefCount(1) {
printf("(%p)Foo ::Foo()\n", (void*)this);

~Foo() throw() {
printf("(%p)Foo ::~Foo()\n", (void*)this);

static void RefCount_XAdd(F oo* const This, int Count) {
if (This) {
printf("(%p)Foo ::RefCount_XAdd (%d)\n", (void*)This, Count);
if (This->m_RefCount < 1) {

void Test(int State) {
printf("(%p)Foo ::Test(%d)\n", (void*)this, State);
int main() {
Foo::Handle Foo1 = Foo::Create();

Foo::Handle Foo2 = Foo1;

Foo::Handle Foo3 = Foo1;
Foo::Handle Foo4 = Foo2;


printf("\n\n___ _______________ __\npress <ENTERto exit...\n");
return 0;

Is this legitimate? Or, is the idea basically equal to a pile of pointless
Any thoughts?


May 13 '07 #1
0 1277

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too stupid to understand and make them work. E.g. I have read that boost's smart pointers are able to do this convertion, but the following code doesn't compile (VC++6.0):
by: Tim Clacy | last post by:
Am I correct in think that you can't re-assign a reference to a different object? If so, what should happen here: struct A { DoSomeThing(); }; A& GetNextA();
by: Ben | last post by:
For many times, I've found myself changing my member variables from const back to non-const. No matter how good the original objective was, there was always at least one reason not to use const members. (swap, storing it in a container, etc.) Whereas in Java, 80% of the case, I would want "final" for my instance variables. It makes me think that is "const member variable" ever useful in C++? Maybe because of the value semantics of C++...
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template works for many different types. The one that I use is named Integer and is a Wrapper for an int. The...
by: grubertm | last post by:
When accessing pointer member variables these are not treated as const even though the instance is const. This came as a complete surprise to me and I would appreciate some feedback why that is the case. Here's a snippet: class MyClass { public: char *pChar; }; void ChangePointedTo (char *ch)
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows it. Specifically, part 1 is obvious to most anyone. Part 2 is isomorphic to part 1, yet behaves differently. If a shared_ptr is const, should it really allow non-const dereferences? Thanks,
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.