Inheriting operator []

  • Thread starter Thomas Matthews
  • Start date
T

Thomas Matthews

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
 
J

John Harrison

Thomas Matthews said:
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
 
M

Mike Wahler

Thomas Matthews said:
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
 
T

Thomas Matthews

John said:
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::eek: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
 
T

Thomas Matthews

Mike said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top