Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheriting operator []

Reply
Thread Tools

Inheriting operator []

 
 
Thomas Matthews
Guest
Posts: n/a
 
      03-01-2004
Hi,

I have a class Record which is a container of fields.
I have overloaded operator[] to return a pointer to a Field:
class Field; // Forward declaration
class Record
{
public:
Field * operator[](unsigned int index);
};

I have defined a class, Keyed_Record, which is-a Record
that has Keys associated with it:
class Key;
class Keyed_Record
: public Record
{
std::vector<Key *> keys;
};

My issue is that operator[] is not working for the
Keyed_Record class. I've searched the FAQ and Bruce
Eckel's book and didn't find anything. Unfortunately,
my Stroustup book is at home and my electronic copy
of the standard is on my home computer.

Is there any reason that operator[] is not inherited
by Keyed_Record?

I'm using Borland 6, if that makes any difference.

Here is an application of the operator[] for the
Keyed_Record:

int main(void)
{
Field * pf;
Keyed_Record kr;
pf = kr[1]; // Return pointer to second field
// in keyed record.
return 0;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-01-2004

"Thomas Matthews" <> wrote in
message news:5PK0c.30299$. com...
> Hi,
>
> I have a class Record which is a container of fields.
> I have overloaded operator[] to return a pointer to a Field:
> class Field; // Forward declaration
> class Record
> {
> public:
> Field * operator[](unsigned int index);
> };
>
> I have defined a class, Keyed_Record, which is-a Record
> that has Keys associated with it:
> class Key;
> class Keyed_Record
> : public Record
> {
> std::vector<Key *> keys;
> };
>
> My issue is that operator[] is not working for the
> Keyed_Record class. I've searched the FAQ and Bruce
> Eckel's book and didn't find anything. Unfortunately,
> my Stroustup book is at home and my electronic copy
> of the standard is on my home computer.
>
> Is there any reason that operator[] is not inherited
> by Keyed_Record?


None that I can see from your code snippetts.

>
> I'm using Borland 6, if that makes any difference.
>
> Here is an application of the operator[] for the
> Keyed_Record:
>
> int main(void)
> {
> Field * pf;
> Keyed_Record kr;
> pf = kr[1]; // Return pointer to second field
> // in keyed record.
> return 0;
> }


What do you mean when you say that it doesn't work? Does it compile? If not
what are the error messages. Does it run incorrectly? If so then what goes
wrong?

john


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      03-01-2004

"Thomas Matthews" <> wrote in
message news:5PK0c.30299$. com...
> Hi,
>
> I have a class Record which is a container of fields.
> I have overloaded operator[] to return a pointer to a Field:
> class Field; // Forward declaration
> class Record
> {
> public:
> Field * operator[](unsigned int index);
> };
>
> I have defined a class, Keyed_Record, which is-a Record
> that has Keys associated with it:
> class Key;
> class Keyed_Record
> : public Record
> {
> std::vector<Key *> keys;
> };
>
> My issue is that operator[] is not working for the
> Keyed_Record class. I've searched the FAQ and Bruce
> Eckel's book and didn't find anything. Unfortunately,
> my Stroustup book is at home and my electronic copy
> of the standard is on my home computer.
>
> Is there any reason that operator[] is not inherited
> by Keyed_Record?
>
> I'm using Borland 6, if that makes any difference.
>
> Here is an application of the operator[] for the
> Keyed_Record:
>
> int main(void)
> {
> Field * pf;
> Keyed_Record kr;
> pf = kr[1]; // Return pointer to second field
> // in keyed record.
> return 0;
> }



I've made changes and additions in order to make my
guesses at what you're doing testable:

#include <iostream>
#include <string>
#include <vector>

class Field
{
public:
std::string data;
Field(const std::string& arg) : data(arg) { }
};

class Record
{
std::vector<Field> fields;
public:
Record()
{
fields.push_back(Field("one"));
fields.push_back(Field("two"));
fields.push_back(Field("three"));
}

Field * operator[](unsigned int index)
{
return &fields[index];
}
};

class Key;
class Keyed_Record
: public Record
{
std::vector<Key *> keys;
};

int main(void)
{
Field * pf;
Keyed_Record kr;
pf = kr[1]; // Return pointer to second field
// in keyed record.

std::cout << kr[1]->data << '\n';
return 0;
}

Output:

two


-Mike


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      03-02-2004
John Harrison wrote:
> "Thomas Matthews" <> wrote in
> message news:5PK0c.30299$. com...
>
>>Hi,
>>
>>I have a class Record which is a container of fields.
>>I have overloaded operator[] to return a pointer to a Field:
>>class Field; // Forward declaration
>>class Record
>>{
>> public:
>> Field * operator[](unsigned int index);
>>};
>>
>>I have defined a class, Keyed_Record, which is-a Record
>>that has Keys associated with it:
>>class Key;
>>class Keyed_Record
>> : public Record
>>{
>> std::vector<Key *> keys;
>>};
>>
>>My issue is that operator[] is not working for the
>>Keyed_Record class. I've searched the FAQ and Bruce
>>Eckel's book and didn't find anything. Unfortunately,
>>my Stroustup book is at home and my electronic copy
>>of the standard is on my home computer.
>>
>>Is there any reason that operator[] is not inherited
>>by Keyed_Record?

>
>
> None that I can see from your code snippetts.
>
>
>>I'm using Borland 6, if that makes any difference.
>>
>>Here is an application of the operator[] for the
>>Keyed_Record:
>>
>>int main(void)
>>{
>> Field * pf;
>> Keyed_Record kr;
>> pf = kr[1]; // Return pointer to second field
>> // in keyed record.
>> return 0;
>>}

>
>
> What do you mean when you say that it doesn't work? Does it compile? If not
> what are the error messages. Does it run incorrectly? If so then what goes
> wrong?
>
> john


Mea culpa! My mistake, I declared the Record:perator[] as:
Field * operator[](const string& s);
The operator that I was asking for was:
Field * operator[](unsigned int i);

{I even searched the ISO spec and found no reason preventing
inheritance of the operator[].}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      03-02-2004
Mike Wahler wrote:

> "Thomas Matthews" <> wrote in
> message news:5PK0c.30299$. com...
>
>>Hi,
>>
>>I have a class Record which is a container of fields.
>>I have overloaded operator[] to return a pointer to a Field:
>>class Field; // Forward declaration
>>class Record
>>{
>> public:
>> Field * operator[](unsigned int index);
>>};
>>
>>I have defined a class, Keyed_Record, which is-a Record
>>that has Keys associated with it:
>>class Key;
>>class Keyed_Record
>> : public Record
>>{
>> std::vector<Key *> keys;
>>};
>>
>>My issue is that operator[] is not working for the
>>Keyed_Record class. I've searched the FAQ and Bruce
>>Eckel's book and didn't find anything. Unfortunately,
>>my Stroustup book is at home and my electronic copy
>>of the standard is on my home computer.
>>
>>Is there any reason that operator[] is not inherited
>>by Keyed_Record?
>>
>>I'm using Borland 6, if that makes any difference.
>>
>>Here is an application of the operator[] for the
>>Keyed_Record:
>>
>>int main(void)
>>{
>> Field * pf;
>> Keyed_Record kr;
>> pf = kr[1]; // Return pointer to second field
>> // in keyed record.
>> return 0;
>>}

>
>
>
> I've made changes and additions in order to make my
> guesses at what you're doing testable:
>
> #include <iostream>
> #include <string>
> #include <vector>
>
> class Field
> {
> public:
> std::string data;
> Field(const std::string& arg) : data(arg) { }
> };
>
> class Record
> {
> std::vector<Field> fields;
> public:
> Record()
> {
> fields.push_back(Field("one"));
> fields.push_back(Field("two"));
> fields.push_back(Field("three"));
> }
>
> Field * operator[](unsigned int index)
> {
> return &fields[index];
> }
> };
>
> class Key;
> class Keyed_Record
> : public Record
> {
> std::vector<Key *> keys;
> };
>
> int main(void)
> {
> Field * pf;
> Keyed_Record kr;
> pf = kr[1]; // Return pointer to second field
> // in keyed record.
>
> std::cout << kr[1]->data << '\n';
> return 0;
> }
>
> Output:
>
> two
>
>
> -Mike
>
>


My mistake. The method defined in my Record was actually:
Field * operator[](const string& field_name);
The method causing the problems was:
Field * operator[](unsigned int index);

Perhaps I need to walk around the building before posting.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
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
inheriting the operator<< johnmmcparland C++ 6 03-23-2007 07:58 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Inheriting operator() and templates Drew McCormack C++ 3 07-12-2004 02:25 PM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57