views:

22

answers:

3

hi,

I am new to C++ and i have a question on overloading dereference operator. I am building a in memory object store which is to be used by the applications. The data store is mapped in to the applications memory space and applications can directly read/modify the object using dereference operator . I plan to provide an interface described below

       DsObject *obj = get_ds_object_ref ("Junk"); // get reference to the object
       int  i = obj->value; // obj is read locked and the value is copied to i variable
       obj->value = i; // obj is write locked and i is copied to the field "value"

In the operator overload function is there a way to know whether the object is being read or written ? suggestions are welcome.

A: 

I suggest you to use get/set functions, they provide required operation type: read or write.

Alex Farber
A: 

No, I don't think so. It is not possible (AFAIK) to determine how obj->value is being used. Perhaps you will need something like this:

DsObject *obj = get_ds_object_ref ("Junk");
int i = GetValue(obj);
SetValue(obj,i);

I'm sorry that there is not much more you can do.

Alexander Rafferty
A: 

To achive a lock to the object you need an additional proxy object. Let the derefence (o any other) operator return the proxy that provides a cast opeator to get the intenden type. The lifetime of the proxy may be used to define a lock.

harper