tags:

views:

113

answers:

5

Suppose I have two instances of the same class. The class has a pointer to some data, and I want the instances to exchange the pointers as part of some private function's algorithm, but without compromising the data to everybody else by giving a direct access to it through a public function.

My first idea was to add a static variable of type bool called exchange, and two methods: a private one: void requestExchange() and a public one: Data** respond(). requestExchange would set exchange to true, and will be immediately followed with respond() from the instance of choice, which will do the following:

if(exchange==true){
    exchange=false;
    return data;
}
else{return...?!

This was when I realized that I have no way of simulating "NO OP" since data may in fact be NULL, so supposedly everything goes as a response. Any ideas as to what can be done?

UPDATE: I thought a bit about it, and since the request-respond combo will only be called in the context where NULL as a result will be meaningful (exchange is most certainly true) I suppose I can simply return NULL and simulate a NO OP this way. To an outsider NULL as a result will be useless... But I'm still interested in suggestions. There must be a more well structured way of doing this.

+3  A: 

Objects of the same class can access each others' private data directly. You often see this in copy constructors, for example.

marijne
*face palm*OK Thanks... I guess my memory of C++ is worse than I thought..
Epsilon Vector
A: 

Your description of the problem is not very clear. Why can't you just make respond() a private function?

The idea with a static class member is fraught with peril. What if two pairs of such instances want to communicate simultaneously? What if one sets the exchange flag and then dies before it comes around to calling respond()? What about thread safety?

As for returning a NO-OP or error indicator, you can either use exceptions (that's what they are for, but if your project does not use exceptions it's not a good idea to introduce them suddenly), or go the route of boost::optional.

atzz
A: 

Might be best to separate your concerns with regards to returning the data and exchanging it.

class Foo
{
public:
   Bar* data()
   {
      return pData;
   }
private:
   void exchangeData(Foo& Rhs)
   {
      if (this != &Rhs)
      {
         Bar* pTmp = pData;
         pData = Rhs.pData;
         Rhs.pData = pTmp;
      }
   }
   Bar* pData;
}

Hopefully it's along the lines of what you want? The question isn't super clear....

Snazzer
The exchangeData() function can be public.
TonJ
I believe his main concern is that you can only do the exchange from a private context.
Snazzer
A: 

I probably missed the point of your question. Why does this not do what you want?

class CMyClass  
{
public:
    void ExchangePointerWith( CMyClass& rhs );

private:
    void* m_MyPtr;
};

and:

void CMyClass::ExchangePointerWith(CMyClass &rhs)
{
    void* tmp= m_MyPtr;
    m_MyPtr= rhs.m_MyPtr;
    rhs.m_MyPtr= tmp;
}
TonJ
A: 

Use std::swap() and build your class's own swap method then you know it should be exception safe. And swap() is a standard routing that most class should implement to make them efficient for the STL.

Remember that a class is automatically a friend of itself. So it can access the private member variables of another instance of the same class. See (http://stackoverflow.com/questions/437250/friend-scope-in-c/437507#437507)

#include <algorithm>

class myX
{
    public:
        void swap(myX& rhs) throw()
        {
            std::swap(data,rhs.data);
        }
    private:
        void* data;
 };
Martin York