Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overload operator[] How ?

1 view
Skip to first unread message

Asger Jørgensen

unread,
Mar 18, 1999, 3:00:00 AM3/18/99
to
Hi there

Could someone please be so kind as to explain what i am doing wrong .

I am making a list class for use in a little card game and i want to
overload the [] operator.

In the .h file i do like this:

class TListt{
private:
static TListt* Shared;
int TheEnd;
TCard** Lst;

protected:
public:

//Other functions functions

TCard* operator[] (unsigned Index) {return Lst[Index];}

bool __fastcall AddShared();
};

in the .cpp file i have a function where i try to use the operator
but i recive the error that TListt can't be converted to TCard*
WHY ?? and how do i solve it ??

bool __fastcall TListt::AddShared()
{
TCard* R = Shared[0]; //<<<<<<<<<<<<<<<<<<<

//........Snip

Thanks in advance
Asger
Asg...@get2net.dk


Scott Heiman

unread,
Mar 18, 1999, 3:00:00 AM3/18/99
to
Asger Jørgensen wrote in message <36F0E272...@get2net.dk>...
<snip>

Shared was declared as a TListt *. Lst was declared as a TCard **.

So this

> TCard* R = Shared[0]; //<<<<<<<<<<<<<<<<<<<


should be

TCard* R = Lst[0];

or

TListt &R = Shared[0];

BTW, this looks like Builder code. You should post builder questions in the
newsgroups devoted to Builder (borland.public.cppbuilder.language).

--
Regards,
Scott
http://www.gnt.net/~heiman


Asger Jørgensen

unread,
Mar 18, 1999, 3:00:00 AM3/18/99
to Scott Heiman
Hi Scott

Thanks for replying.

Scott Heiman wrote:

> Asger Jørgensen wrote


> <snip>
>
> Shared was declared as a TListt *. Lst was declared as a TCard **.
>
> So this
>
> > TCard* R = Shared[0]; //<<<<<<<<<<<<<<<<<<<
>
> should be
>
> TCard* R = Lst[0];
>
> or
>
> TListt &R = Shared[0];

but i have overloaded the [] oprator like this :

TCard* __fastcall operator[](unsigned Index){return Lst[Index];}

so i thought Shared[0] would return the first TCard* from the
TListt Lst
or have i misunderstood the thing about overloading?
I must edmit that i am new to the overload stuff.

> BTW, this looks like Builder code. You should post builder questions in the
> newsgroups devoted to Builder (borland.public.cppbuilder.language).

It is but i thought that this was a C++ language question since it is
a simple class - no component stuff.

Thanks again
Asger
asg...@get2net.dk


Scott Heiman

unread,
Mar 18, 1999, 3:00:00 AM3/18/99
to
Asger Jørgensen wrote in message <36F0F304...@get2net.dk>...
<snip>

>
>but i have overloaded the [] oprator like this :
>
> TCard* __fastcall operator[](unsigned Index){return Lst[Index];}
>

This is perfectly legitimate. For example

TListt A;

TCard *B = A[1];

should work.

>so i thought Shared[0] would return the first TCard* from the
>TListt Lst

Shared[0] will return the first TListt object in the array pointed to by
Shared.

>or have i misunderstood the thing about overloading?


I think so. The overloaded [] operator works for a TListt object as in the
example above; however,

TCard *B = Shared[0][0];

will do what you want. This returns the first card in the first TListt
object.

>> BTW, this looks like Builder code. You should post builder questions in
the
>> newsgroups devoted to Builder (borland.public.cppbuilder.language).
>
>It is but i thought that this was a C++ language question since it is
>a simple class - no component stuff.

OK, you're forgiven ;-)

Asger Jørgensen

unread,
Mar 19, 1999, 3:00:00 AM3/19/99
to
Hi Scott

Scott Heiman wrote:

> I think so. The overloaded [] operator works for a TListt object as in the
> example above; however,
>
> TCard *B = Shared[0][0];
>
> will do what you want. This returns the first card in the first TListt
> object.

Yeb! and thats where i went wrong, i almost only work with pointer
to objects so i newer thought about that Shared only was a pointer and
not the object.
So:

TCard *B = (*Shared)[0];

will do to, but both wayes kindof messes up elegant the syntaks so i will
probably see if i can figure out how to make a syntax like this:


TCard *B = Shared->Items[0];

if not i will use the standard function call:

TCard *B = Shared->Items(int Index);

Thanks again
Asger
Asg...@get2net.dk


0 new messages