By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,011 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,011 IT Pros & Developers. It's quick & easy.

Pointer proxying with reference members

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jim Campbell <ji*@rochester.rr.com> wrote in message
news:Lw******************@twister.nyroc.rr.com...
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

Jul 19 '05 #2

P: n/a
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

"David White" <no@email.provided> wrote in message news:<hY******************@nasal.pacific.net.au>.. .
Jim Campbell <ji*@rochester.rr.com> wrote in message
news:Lw******************@twister.nyroc.rr.com...
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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.