How to convert a CObject* back to it's complex type source

 

I'm trying access a CObject* of a CArrayObj array but I can't seem to put it back into the structure that was originally placed there.

My complex type is:

   class myobj_t : public CObject
     {
   public:
      double         a;
      datetime       b;

                     myobj_t(void)
         :           a(0.0), b(0) {};
                     myobj_t(const myobj_t& src)
         :           a(src.a), b(src.b) {};
     };

And I'm accessing it with At like so.

myobj_t mo = new myobj_t();

myobj_t t = mo.At(0);
PrintFormat(__FUNCTION__+", t = %f", t.a);

delete mo;
It's when I access the CObject* in t,a that the indicator crashes so how do I convert it back to myobj_t. I've tried casting but that didn't work. Any help appreciated. Thanks.
 
You have no At method. You have no pointers in your class. Don't post code that won't even compile.
     How To Ask Questions The Smart Way. 2004
          Be precise and informative about your problem
 
William Roeder:
You have no At method. You have no pointers in your class. Don't post code that won't even compile.
     How To Ask Questions The Smart Way. 2004
          Be precise and informative about your problem

Sorry, this is the code that fails:

#include <Arrays/ArrayObj.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class test_t : public CObject
  {
public:
   double            a;
   double            b;
   datetime          c;

                     test_t(void)
      :              a(0.0), b(0.0), c(0) {};
                     test_t(const test_t& src)
      :              a(src.a), b(src.b), c(src.c) {};
  };

CArrayObj *obj = new CArrayObj;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   obj.Add(&build_struct(1.0,2.0,iTime(NULL,0,0)));

   const test_t *r = obj.At(0);

   if(r == NULL)
      PrintFormat(__FUNCTION__+", NULL");

   PrintFormat("obj[0].a = %f", r.a);

   delete obj;
  }
//+------------------------------------------------------------------+
test_t build_struct(const double a, const double b, const datetime c)
  {
   test_t t;
   t.a = a;
   t.b = b;
   t.c = c;

   return t;
  }

I think it is the build_struct function that is creating the problem. How can I correct it to function correctly?

 
imamushroom:

Sorry, this is the code that fails:

I think it is the build_struct function that is creating the problem. How can I correct it to function correctly?

Try this:

#include <Arrays/ArrayObj.mqh>

class test_t : public CObject
  {
public:
   double            a;
   double            b;
   datetime          c;

                     test_t(void)
      :              a(0.0), b(0.0), c(0) {};
                     test_t(const test_t& src)
      :              a(src.a), b(src.b), c(src.c) {};
  };

CArrayObj *obj = new CArrayObj;

void OnStart()
  {
   obj.Add(build_struct(1.0,2.0,iTime(NULL,0,0)));
   const test_t *r = obj.At(0);
   if(r == NULL)
      PrintFormat(__FUNCTION__+", NULL");
   PrintFormat("obj[0].a = %f", r.a);
   delete obj;
  }
//+------------------------------------------------------------------+
test_t* build_struct(const double a, const double b, const datetime c)
  {
   test_t *t=new test_t();
   t.a = a;
   t.b = b;
   t.c = c;
   return t;
  }
 
That works, thanks.
 
Don't create an external build_struct — make a constructor.
                     test_t(const double aA=0, const double aB=0, const datetime aC=0)
      :              a(aA), b(aB), c(aC) {}
Reason: