0

I have a class to read from/write to an external random access memory.

.h file:

class RAM {
private:
  bool Read(char *Buffer, size_t BytesToRead, uint32_t StartAddress);
  bool Write(char *Buffer, size_t BytesToWrite, uint32_t StartAddress);

  class proxy {
  private:
    size_t _index;
  public:
    proxy(size_t i);
    void operator=(char rhs);
    operator char();
  }

public:
  proxy operator[](size_t i);
};

.cpp file:

RAM::proxy RAM::operator[](size_t i) {
  return proxy(i);
}

proxy::proxy(size_t i) {
    _index = i;
}

void proxy::operator=(char rhs) {
  Write(&rhs, 1, _index);  // error
}

RAM::proxy::operator char() {
  char Buf[1];
  Read(Buf, 1, _index);   // error
  return Buf[0];
}

Read and Write are functions that deal with low-level access to the RAM. There is no other way to read from or write to the RAM. I tried to use proxy class in rvalue - how to implement assignment operator? but couldn't adapt it without error:

error: cannot call member function 'bool RAM::Write(char*, size_t, uint32_t)' without object Write(&rhs, 1, _index)

error: cannot call member function 'bool RAM::Read(char*, size_t, uint32_t)' without object Read(Buf, 1, _index)

There are other things calling on Read and Write, so I probably can't make them static. Also I thought they are already instantiated when a RAM object is created.

Community
  • 1
  • 1
Alex
  • 123
  • 4
  • How can you return an object of private type from a public function? – SergeyA Jan 29 '18 at 22:17
  • 1
    You are calling `RAM::Write` from class `proxy` of which it is not a member. – Richard Critten Jan 29 '18 at 22:18
  • Your `Read`, `Write` functions should be static. – llllllllll Jan 29 '18 at 22:19
  • 1
    There is no inherent connection between an object of type `proxy` and an object of type `RAM`. In order to call a member function of a `RAM` object the `proxy` object has to have a pointer or reference to a `RAM` object. Just add one, and initialize it when the `proxy` object is created. – Pete Becker Jan 29 '18 at 22:20
  • @RichardCritten Class proxy is a subclass of RAM class. It should have access to all of RAM class's functions and variables. – Alex Jan 29 '18 at 22:27
  • Nope that's Java; C++ does not work like that see @PeteBecker comment. There is no difference (except for the name) between a top level class and a nested class declaration. – Richard Critten Jan 29 '18 at 22:28
  • @PeteBecker Thanks!! Took me a while to figure out how to pass parent object pointer, but it works! – Alex Jan 29 '18 at 22:48
  • @SergeyA [That seems to be allowed.](http://coliru.stacked-crooked.com/a/416ca49b55d9e978) – HolyBlackCat Jan 29 '18 at 22:52

1 Answers1

1

Thanks to PeteBecker I was able to come up with a working code. Also, I didn't ask in the original question, but I neglected to test direct assignment, which turns out to be a different beast.

class RAM {
private:
  // changed 1st arg to void*, because type doesn't really matter.
  bool Read(void *Buffer, size_t BytesToRead, uint32_t StartAddress);
  bool Write(void *Buffer, size_t BytesToWrite, uint32_t StartAddress);

  class proxy {
  private:
    size_t _index;
    RAM *_parentPtr;  // <-- added

  public:
    proxy(size_t i, RAM *ptr) {
      _index = i;
      _parentPtr = ptr;  // <-- added
    }  
    void operator=(char rhs) {
      Write(&rhs, 1, _index);  // works now
    }
    void operator=(proxy rhs) {  // new function for direct assignment
      char temp = rhs;
      Write(&temp, 1, _index);
    }
    operator char() const {
      char Buf[1];
      Read(Buf, 1, _index);   // works now
      return Buf[0];
    }
  }

public:
  proxy operator[](size_t i) {
    return proxy(i, this);
  }
};

Took me a long time to figure out that I must request the value of the rhs via a temporary variable to execute operator char() const. Passing as reference won't do it.

Alex
  • 123
  • 4