473,804 Members | 3,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer proxying with reference members

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
2 2447
Jim Campbell <ji*@rochester. rr.com> wrote in message
news:Lw******** **********@twis ter.nyroc.rr.co m...
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
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.provi ded> wrote in message news:<hY******* ***********@nas al.pacific.net. au>...
Jim Campbell <ji*@rochester. rr.com> wrote in message
news:Lw******** **********@twis ter.nyroc.rr.co m...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1859
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in include files: typedef Context *Context_ptr; typedef ObjOut<Context> Context_out;
4
4741
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
2
1577
by: pico | last post by:
I am attempting to write an app that allows me to reference class members from a structure. There is only one instance of this class so I have no problem making the members (obj1, obj2, etc.) static. I can get build the structure fine but Im having trouble dereferencing it. My app runs under CE so I would really like the structure to be a const to keep it in rom. Here is a simplified version of my code. Can someone help me out? ...
14
7190
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your opinion? Vol
2
1732
by: Howard | last post by:
Hi, I've got a program whose main object contains an array of "cells", each of which contains an array of "sub-cells", each of which contains an array of "sub-sub-cells". The cells, subcells and subsubcells are three user-defined types, each of which contains some program logic as well as some drawing capabilities. Since all cells (and subcells and subsubcells) will utilize identical drawing "settings", I'm using a single settings...
18
1825
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
41
3689
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what pointer and reference are and how they relate to each other.
5
4666
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
50
4521
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
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 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...
1
10319
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,...
1
7616
isladogs
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...
0
6851
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();...
0
5520
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...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.