473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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<typename T>
class AutoRef {
mutable T *m_State;

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

~AutoRef() throw() {
T::RefCount_XAdd(m_State, -1);
printf("(%p)AutoRef::~AutoRef\n", (void*)this);
}

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

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

public:
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<FooAutoRef_t;

private:
int m_RefCount;

public:
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);
}

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

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

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

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

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

Foo::Handle Foo2 = Foo1;
Foo2->Test(2);

{
Foo::Handle Foo3 = Foo1;
Foo::Handle Foo4 = Foo2;
Foo3->Test(3);
Foo4->Test(4);
}

Foo1->Test(5);
Foo2->Test(6);
}

printf("\n\n____________________\npress <ENTERto exit...\n");
getchar();
return 0;
}

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

:^)

May 13 '07 #1
0 1263

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

Similar topics

5
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...
29
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();
31
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...
10
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...
23
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...
0
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,...
29
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...
14
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...
68
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.