How to declare class inside class?

 

Hi, I would like to have the following structure:

template <typename T>
class RingBuffer {
  public:
  class AccessResult 
  {
    public:
    bool valid;
    T item;
  };
  AccessResult getAt(idx i) const;
};

With this, I can declare a RingBuffer like this:

RingBuffer<SomeFancyElement> elems;

and then access it safely via

RingBuffer<SomeFancyElement>::AccessResult res = elems.getAt(2);
if (res.valid) { do something with res.item of Type SomeFancyElement };


Unfortunatelly, I get a compiler error which says 

"AccessResult" struct member undefined.


Is there some work-around?

Thank you very much!

 
Eugen Funk:
AccessResult getAt(idx i) const;

If it is a method, what is the return type?

And what is (idx i)? Is it 2 arguments or is idx a data type?

 

This is compiling without error:

template <typename T>
class RingBuffer {
  public:
  class AccessResult 
  {
    public:
    bool valid;
    T item;
  };
  AccessResult getAt(int i) const;
};

void f()
{
   RingBuffer<int>::AccessResult res;
}
 
Yashar Seyyedin #:

This is compiling without error:

I modified your version so that it can return a pointer:

template <typename T>
class RingBuffer {
  public:
  class AccessResult 
  {
    public:
    bool valid;
    T item;
  };
  static AccessResult* getAt(int) { return(NULL); }
};

void OnStart()
  {
   RingBuffer<int>::AccessResult *res = RingBuffer<int>::getAt(123);
  }
 

It looks like I was able to make a copy of the object using the auto pointer while doing so.

But to be honest, I've never used template before😄 This is my first experience with template.

template <typename T>
class RingBuffer{
  public:
  class AccessResult 
  {
    public:
    bool valid;
    T item;
    AccessResult() { Print(__FUNCSIG__, " ", EnumToString(CheckPointer(GetPointer(this)))); }
    AccessResult(AccessResult& a)
      {
       valid = a.valid;
       item = a.item;
       Print(__FUNCSIG__, " ", EnumToString(CheckPointer(GetPointer(this))));
      }
  };
  AccessResult* someInstance;
  AccessResult* getInstance() { return(someInstance);            }
                RingBuffer()  { someInstance = new AccessResult; }
                ~RingBuffer() { delete someInstance;             }
};

void OnStart()
  {
   RingBuffer<int> buffer;
   RingBuffer<int>::AccessResult objectCopy(buffer.getInstance());
  }

Reason: