How to pointer to the structure array

 

Hi all

I have meet an issue, anyone suggest me!

as

class CDemoClass
  {
public:
   double            m_array;
   double            m_array2;
  };
CDemoClass demo[];

How to make poiter to m_array and m_array2 as like:

demo[0]."m_array"=1;  demo[0]."m_array2"=2;

 
  1. demo[0]."m_array"=1;  demo[0]."m_array2"=2;
    Why did you put quotes around your variable name?
  2. MTx does not have pointers — it has handles to class objects only.
 
Nguyen Nguyen Khue:

Hi all

I have meet an issue, anyone suggest me!

as

How to make poiter to m_array and m_array2 as like:

demo[0]."m_array"=1;  demo[0]."m_array2"=2;

I think you mean pointers to the Class objects in which case this script will demonstrate how to do it. (Yes you can have pointers)

I have added a method to the class to demonstrate it works

Important that you delete the pointers at the end otherwise you will get memory leaks.

Also, important to check the pointer is valid before you delete it otherwise it can have undesired results.


#property strict

class CDemoClass
{
   public:
      double            m_array;
      double            m_array2;  
      void OutputValue() {Alert("m_array " + m_array + "   m_array2 " + m_array2);} 
   
};

CDemoClass *demo[5]; // declares an array of pointers to CDemoClass object - does not create anything

void OnStart()
{ 
   int iC;
   for(iC=0; iC<5; iC++)
   {
      demo[iC] = new CDemoClass;       // creates an object of the class and stores a pointer to it in the array.
      demo[iC].m_array = 100.0 + iC;   // assigns value
      demo[iC].m_array2 = 200.0 + iC;  // assigns value
      demo[iC].OutputValue();          // called class method 
   }

   for(iC=0; iC<5; iC++)
      if(!CheckPointer(demo[iC]) == POINTER_INVALID)
         delete demo[iC];     
}


 
Paul Anscombe:

I think you mean pointers to the Class objects in which case this script will demonstrate how to do it. (Yes you can have pointers)

I have added a method to the class to demonstrate it works

Important that you delete the pointers at the end otherwise you will get memory leaks.

Also, important to check the pointer is valid before you delete it otherwise it can have undesired results.


Thank you!

 
William Roeder:
  1. Why did you put quotes around your variable name?
  2. MTx does not have pointers — it has handles to class objects only.

1. I known it cannot put quote in variable, so that i want use the variable name and pointing to the variable.

2. I want use a pointer to point to the the variable name in object.

 
Nguyen Nguyen Khue: 2. I want use a pointer to point to the the variable name in object.

What part of “handles to class objects only” was unclear to you?

Reason: