473,386 Members | 1,827 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,386 software developers and data experts.

std::Vector, SmartPointer and const_cast

Hi Ng !

I wrote a SmartPointer class which holds a doubled linked list to all its
copies to ensure that all copies are holding the same pointer, even if
one copy changes later.
If the last SmartPointer in the list is destructed it deletes the
underlying pointer. Well at least it seemed worth trying, because i then
could make a vector of SmartPointers.
Now I wanted to make a vector<SmartPointer<someType> >. The problem is
that vector::push_back wants a const T& as argument, which i cannot
provide, because the Copy-Constructor changes the source Object (to
maintain the double linked list).

I think about providing a const copy-constructor (like the one i have
already) and doing a const_cast in it, but this does not seem to be a
good solution.
Is it safe ? I mean the source should be a valid Object at the time, the
copy-Constructor is called, doesn't it ?
Can anyone give me a hint on how to do it, and correct errors I might
have overseen in this implementation of SmartPointer ?:

Thanks a lot,
Tobias Kilian

template<typename Type> class SmartPointer
{
public:
/// \brief Default Constructor
SmartPointer() : mPointer(0), mNextCopy(0), mPrevCopy(0)
{ };

explicit SmartPointer(Type* pointer) : mPointer(pointer), mNextCopy
(0), mPrevCopy(0)
{
};

/// \brief Copy Constructor
SmartPointer( SmartPointer& source)
:mPointer(source.mPointer)
{
if (!source.mNextCopy)
{
mPrevCopy = &source;
mNextCopy = 0;
source.mNextCopy = this;
}
else
{
debugPrintAll();
mPrevCopy = &source;
mNextCopy = source.mNextCopy;
source.mNextCopy = this;
assert(mNextCopy);
mNextCopy->mPrevCopy = this;
}
}

/// \brief Copy Constructor in Question
SmartPointer( const SmartPointer& src)
:mPointer(src.mPointer)
{
SmartPointer& source = const_cast<SmartPointer&>(src);
if (!source.mNextCopy)
{
mPrevCopy = &source;
mNextCopy = 0;
source.mNextCopy = this;
}
else
{
mPrevCopy = &source;
mNextCopy = source.mNextCopy;
source.mNextCopy = this;
mNextCopy->mPrevCopy = this;
}
}
/// \brief Assignement operator
SmartPointer& SmartPointer::operator= (SmartPointer& source)
{
if (mPointer != source.mPointer)
{
// First of all, delete the old pointer.
delete mPointer;

// Propagate pointer to previous copies
if (mPrevCopy!=0)
{
// rekursiv !
mPrevCopy->assignToPrev(source.mPointer);
}

// The new pointer is taken.
mPointer = source.mPointer;

// Propagate pointer to next copies
if (mNextCopy!=0)
{
mNextCopy->assignToNext(source.mPointer);
}

// Join the lists of this and of the source SmartPointer.
SmartPointer* lastCopy = getLastCopy();
SmartPointer* firstCopy = source.getFirstCopy();
lastCopy->mNextCopy = firstCopy;
firstCopy->mPrevCopy = lastCopy;
}
return *this;
}

SmartPointer* getFirstCopy()
{
if (mPrevCopy)
{
return mPrevCopy->getFirstCopy();
}
else
{
return this;
}
}

SmartPointer* getLastCopy()
{
if (mNextCopy)
{
return mNextCopy->getLastCopy();
}
else
{
return this;
}
}

/// \brief Destructor
~SmartPointer()
{
if (mPrevCopy==0 && mNextCopy==0)
{
delete mPointer;
}
else
{
if (mPrevCopy)
{
mPrevCopy->mNextCopy = mNextCopy;
}

if (mNextCopy)
{
mNextCopy->mPrevCopy = mPrevCopy;
}
}
};
Type* operator->() const { assert(mPointer); return mPointer;}
Type& operator* () const { assert(mPointer); return *mPointer;}

operator bool() const
{
return mPointer!=0;
}
protected:

void SmartPointer::assignToPrev(Type* pointer)
{
// take the given pointer...
mPointer = pointer;

// and progpagate it to the previous copies
if (mPrevCopy)
{
// rekursiv !
(*mPrevCopy).assignToPrev(pointer);
}
}

void SmartPointer::assignToNext(Type* pointer)
{
// take the given pointer...
mPointer = pointer;
if (mNextCopy) // is there another Next copy ?
{
// rekursiv !
(*mNextCopy).assignToNext(pointer);
}
}

Type* mPointer;
SmartPointer* mPrevCopy;
SmartPointer* mNextCopy;
};
Jul 22 '05 #1
2 2078
On 24 May 2004 15:18:11 GMT, Tobias Kilian
<RE**************@uni-koblenz.de> wrote:
Hi Ng !

I wrote a SmartPointer class which holds a doubled linked list to all its
copies to ensure that all copies are holding the same pointer, even if
one copy changes later.
If the last SmartPointer in the list is destructed it deletes the
underlying pointer. Well at least it seemed worth trying, because i then
could make a vector of SmartPointers.
Sounds like a reference-linked smart pointer to me.
Now I wanted to make a vector<SmartPointer<someType> >. The problem is
that vector::push_back wants a const T& as argument, which i cannot
provide, because the Copy-Constructor changes the source Object (to
maintain the double linked list).
Your copy constructor should take a const T& - remember you can modify
pointed-to values in a const object. e.g.

explicit SmartPointer(Type* pointer)
: mPointer(pointer), mNextCopy(this), mPrevCopy(this)
{
//note next and prev point to this
};

/// \brief Copy Constructor
SmartPointer( SmartPointer const& source)
:mPointer(source.mPointer)
{
//cunning trick:
SmartPointer* nonConstSource = source.mPrev->mNext;
//...
}
I think about providing a const copy-constructor (like the one i have
already) and doing a const_cast in it, but this does not seem to be a
good solution.


You can avoid the const_cast as above. And note that reference linked
smart pointers can be less efficient than reference counted ones in
some circumstances, and that they are unsuitable for using from
multiple threads (unless you add some draconian locking to the class).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
Tobias Kilian <RE**************@uni-koblenz.de> wrote in message news:<Xn*********************************@141.26.6 4.5>...
Hi Ng !

I wrote a SmartPointer class which holds a doubled linked list to all its
copies to ensure that all copies are holding the same pointer, even if
one copy changes later.
If the last SmartPointer in the list is destructed it deletes the
underlying pointer. Well at least it seemed worth trying, because i then
could make a vector of SmartPointers.
Now I wanted to make a vector<SmartPointer<someType> >. The problem is
that vector::push_back wants a const T& as argument, which i cannot
provide, because the Copy-Constructor changes the source Object (to
maintain the double linked list).


Technically, yes. Logically, no. The solution is to make the link
pointers mutable. It is legal to change mutable members, even in const
objects. This way, your copy ctor can again take a const&.

Regards,
Michiel Salters
Jul 22 '05 #3

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

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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...

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.