Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   A bit of an pointer to array of pointers problem (http://www.velocityreviews.com/forums/t806835-a-bit-of-an-pointer-to-array-of-pointers-problem.html)

A 12-11-2011 10:48 PM

A bit of an pointer to array of pointers problem
 
Here is a situation:

TCustomObject *pObjA[] = { Obj1, Obj2, Obj3 };
TCustomObject *pObjB[] = { Obj4, Obj5, Obj6 };
TCustomObject *pObjC[] = { Obj7, Obj8, Obj9 };

for (int i = 0; i < ( sizeof(pObjA) / sizeof(pObjA[0]) ); i++)
{
// do something with pObjA[i]...
}

How would you add a pointer to array of pointers so that if a code needs
array A array (Obj1, Obj2 and Obj3)
but if it needs B or C it needs Obj4-6 or Obj7-9?

If I put a pointer to a pointer like this:

TCustomObject **pObjUsed = pObjA;

Then to get Obj1 I would need to do something like:

pObjUsed-> (here use Obj1)

How would you rewrite this for for loop to be able to use any of 3 arrays?



A 12-11-2011 10:50 PM

Re: A bit of an pointer to array of pointers problem
 
To clarify this a bit, I need a pointer to array not a pointer to a pointer
I guess...



A 12-11-2011 10:56 PM

Re: A bit of an pointer to array of pointers problem
 
> TCustomObject *pObjA[] = { Obj1, Obj2, Obj3 };
> TCustomObject *pObjB[] = { Obj4, Obj5, Obj6 };
> TCustomObject *pObjC[] = { Obj7, Obj8, Obj9 };


Another attempt to explain what I want :)

I need a variable that points to element 1 of pObjA to access Obj1, then,
when I increase this variable to point to element 2 (Obj2).
But if pObjB is needed then the same variable needs to point at element 1 of
pObjB (Obj4) and when increased to point to element 2 (Obj5) etc.



Pavel 12-12-2011 12:29 AM

Re: A bit of an pointer to array of pointers problem
 
A wrote:
>> TCustomObject *pObjA[] = { Obj1, Obj2, Obj3 };
>> TCustomObject *pObjB[] = { Obj4, Obj5, Obj6 };
>> TCustomObject *pObjC[] = { Obj7, Obj8, Obj9 };

>
> Another attempt to explain what I want :)
>
> I need a variable that points to element 1 of pObjA to access Obj1, then,
> when I increase this variable to point to element 2 (Obj2).
> But if pObjB is needed then the same variable needs to point at element 1 of
> pObjB (Obj4) and when increased to point to element 2 (Obj5) etc.
>
>



TCustomObject *pObjUsed = needA() ? &pObjA[0]
: needB() ? &pObjB[0]
: /* assume or assert needC() */ &pObjC[0];


for (int i = 0; i < ( sizeof(pObjA) / sizeof(pObjA[0]) ); i++)
{
// to do something with pObj{A|B|C}[i], refer to pObjUsed[i]

}

Is this what you wanted?

HTH,
-Pavel

A 12-12-2011 03:19 AM

Re: A bit of an pointer to array of pointers problem
 

"Pavel" <pauldontspamtolk@removeyourself.dontspam.yahoo> wrote in message
news:4ee54af6$0$31559$c3e8da3$9b4ff22a@news.astraw eb.com...
> TCustomObject *pObjUsed = needA() ? &pObjA[0]
> : needB() ? &pObjB[0]
> : /* assume or assert needC() */ &pObjC[0];
> Is this what you wanted?


that would be that except that my compiler reports that it cannot convert
object type:
it is C++ Builder - Cannot convert 'TNotifyEvent' to 'TNotifyEvent' (if I
use TNofifyEvent instead of TCustomObject).

I think this is compiler specific so I'll look for more help elsewhere.
Thanks for the answer though.



Pavel 12-12-2011 04:48 AM

Re: A bit of an pointer to array of pointers problem
 
A wrote:
> "Pavel"<pauldontspamtolk@removeyourself.dontspam.y ahoo> wrote in message
> news:4ee54af6$0$31559$c3e8da3$9b4ff22a@news.astraw eb.com...
>> TCustomObject *pObjUsed = needA() ?&pObjA[0]
>> : needB() ?&pObjB[0]
>> : /* assume or assert needC() */&pObjC[0];
>> Is this what you wanted?

>
> that would be that except that my compiler reports that it cannot convert
> object type:
> it is C++ Builder - Cannot convert 'TNotifyEvent' to 'TNotifyEvent' (if I
> use TNofifyEvent instead of TCustomObject).
>
> I think this is compiler specific so I'll look for more help elsewhere.
> Thanks for the answer though.
>
>

Yeah, I was wrong, it should have been


TCustomObject **pObjUsed = needA() ? &pObjA[0]
: needB() ? &pObjB[0]
: /* assume or assert needC() */ &pObjC[0];


and then refer to pObjUsed[i] for a pointer or *pObjUsed[i] for an object


All times are GMT. The time now is 04:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.