Pointer proxying with reference members

J

Jim Campbell

Suppose I wanted to a class to contain a pointer, but I didn't want to
make the copy constructor or assignment operator reinitialize this
pointer. (Think of a class that contains an mmapped pointer - having to
duplicate the memory could be very expensive.) Instead, I wanted only
the non-copy construction to create the pointer, and have copies of the
object, achieved through either copy construction or assignment, refer
to the pointer through a reference; in other words, "proxy" the original
object. If an original object changes its pointer, all copy constructed
or reassigned objects will see the update with minimal overhead.

I developed this simple class below to demonstrate how this could be done:

class PtrClass
{
public:
PtrClass(char *p = NULL) : ptr_(p), cpref_(ptr_) { }
PtrClass(const PtrClass &co) : ptr_(NULL), cpref_(co.cpref_) { }

PtrClass &
operator =(const PtrClass &rhs)
{
if (this != &rhs)
{
memcpy(this, &rhs, sizeof(*this));
ptr_ = NULL;
}
return *this;
}

char *getPtr() const { return cpref_; }

~PtrClass() { ptr_ = NULL; }

private:
char *ptr_;
char * &cpref_;
};


My question now: Is there any way within the C++ language to implement
the assignment operator so that the reference is carried from the right
hand side to the destination? Since references can never be reassigned,
the only way I can see around it is by going around C++'s back with the
memcpy().

Thanks,
Jim
 
D

David White

Jim Campbell said:
Suppose I wanted to a class to contain a pointer, but I didn't want to
make the copy constructor or assignment operator reinitialize this
pointer. (Think of a class that contains an mmapped pointer - having to
duplicate the memory could be very expensive.) Instead, I wanted only
the non-copy construction to create the pointer, and have copies of the
object, achieved through either copy construction or assignment, refer
to the pointer through a reference; in other words, "proxy" the original
object. If an original object changes its pointer, all copy constructed
or reassigned objects will see the update with minimal overhead.

I developed this simple class below to demonstrate how this could be done:

class PtrClass
{
public:
PtrClass(char *p = NULL) : ptr_(p), cpref_(ptr_) { }
PtrClass(const PtrClass &co) : ptr_(NULL), cpref_(co.cpref_) { }

PtrClass &
operator =(const PtrClass &rhs)
{
if (this != &rhs)
{
memcpy(this, &rhs, sizeof(*this));

Very nasty.
ptr_ = NULL;
}
return *this;
}

char *getPtr() const { return cpref_; }

~PtrClass() { ptr_ = NULL; }

private:
char *ptr_;
char * &cpref_;
};


My question now: Is there any way within the C++ language to implement
the assignment operator so that the reference is carried from the right
hand side to the destination?

No. You can't make the reference refer to a pointer different from the one
it was initialized to refer to.
Since references can never be reassigned,
the only way I can see around it is by going around C++'s back with the
memcpy().

Why not a pointer to a pointer instead of a reference to a pointer?

DW
 
J

Jim Campbell

Yes, a pointer to a pointer would be the obvious choice for
implementing this "proxy". I just wanted to see if a reference could
get me what I needed.

The "memcpy()" approach is apparently unportable, in that I've only
gotten it to work under the GNU C++ compiler. On the Sun Workshop 6
compiler, it fails.

- Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top