Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A bit of an pointer to array of pointers problem

Reply
Thread Tools

A bit of an pointer to array of pointers problem

 
 
A
Guest
Posts: n/a
 
      12-11-2011
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?


 
Reply With Quote
 
 
 
 
A
Guest
Posts: n/a
 
      12-11-2011
To clarify this a bit, I need a pointer to array not a pointer to a pointer
I guess...


 
Reply With Quote
 
 
 
 
A
Guest
Posts: n/a
 
      12-11-2011
> 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.


 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      12-12-2011
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
 
Reply With Quote
 
A
Guest
Posts: n/a
 
      12-12-2011

"Pavel" <(E-Mail Removed)> wrote in message
news:4ee54af6$0$31559$c3e8da3$(E-Mail Removed) 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.


 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      12-12-2011
A wrote:
> "Pavel"<(E-Mail Removed) ahoo> wrote in message
> news:4ee54af6$0$31559$c3e8da3$(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
32 bit pointer to 64 bit pointer Saurabh Aggrawal C++ 1 12-09-2004 12:01 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments