Click here to Skip to main content
15,924,318 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I want to write a class that returns a pointer to any data in an array:
C++
class CData
{
   private:
      struct
      {
	  void* pVoid;
	  int   iLen;
      }data;

   public:
      void  SetData (void* pVoid);
      void* GetData (int iNum);
};

void CData::SetData (void* pVoid)
{
   data.pVoid = pVoid;
   data.iLen  = iLen;
}

void* CData::GetData (int iNum)
{
   return data.pVoid + iNum*iLen;  //Simplified
}


With normal data arrays this isn't a problem, but for structures like this I need some help:
C++
struct CHILD
{
   TCHAR szString [50];
   int   iInt;
};

struct MASTER
{
   CHILD* pChild;
   TCHAR  szString [50];
   int    iInt;
};

C++
static MASTER master [3];
static CHILD  child;

_tcscpy (child.szString, TEXT ("Child main"));
child.iInt = 12;

master[0].pChild = master[2].pChild = &child;
master[1].pChild = new CHILD;

master[1].pChild->iInt = 11;
_tcscpy (master[1].pChild->szString, TEXT ("Child 1"));
_tcscpy (master[0].szString, TEXT ("Master 0"));
_tcscpy (master[1].szString, TEXT ("Master 1"));
_tcscpy (master[2].szString, TEXT ("Master 2"));

master[0].iInt = 20;
master[1].iInt = 21;
master[2].iInt = 22;


Returning data from the MASTER structure isn't a problem, but returning data from the CHILD structs is a little bit more complicated because the structs are not "aligned" in the memory(?) So i guesse I have to work with pointers to pointers, but I have no idea how to start, so any ideas?
Posted
Comments
Sergey Alexandrovich Kryukov 17-Oct-11 14:05pm    
Why using direct access to memory at all? This is the abuse of technology.
--SA

Forget structs and use properly encapsulated classes and you will not need to worry about the issue. Your code should not be using direct copy commands to add or remove data from an object, that is asking for trouble in the long term.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Oct-11 14:06pm    
My 5. Looks like OP is lost in basic things. Your explanation is correct, but the difference between classes and structures are really irrelevant here. In C++, they are essentially the same (well, default access modifier is different, so what?).
--SA
Richard MacCutchan 17-Oct-11 14:21pm    
essentially the same ... you and I know that, but it's better for someone new(ish) to stick with the concept(s) of classes.
TimGalant 17-Oct-11 15:21pm    
But when I want to do it as I explained, can it be done and how? Cause the problem with the "alignment" in the memory will be the same, isn't it?
Richard MacCutchan 17-Oct-11 15:27pm    
What do you mean "alignment"? This is not something you need to worry about unless you are dealing with structures whose alignment is critical for some external reason.
TimGalant 17-Oct-11 16:07pm    
I mean that with a normal array you know "where" in the memory you can find the fourth, tentht, fiftieth etc. variable in the array if you have a pointer to the first variable of the array. With the structures I described above you don't know where in the memory you can find the members of the child structs.
You did it already, in this line:
C++
_tcscpy (master[1].pChild->szString, TEXT ("Child 1"));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900