468,765 Members | 944 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Inheriting operator []

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

Jul 22 '05 #1
4 1307

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:5P*******************@newssvr16.news.prodigy. 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
Jul 22 '05 #2

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:5P*******************@newssvr16.news.prodigy. 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
Jul 22 '05 #3
John Harrison wrote:
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:5P*******************@newssvr16.news.prodigy. 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::operator[] 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

Jul 22 '05 #4
Mike Wahler wrote:
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:5P*******************@newssvr16.news.prodigy. 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

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andy Jarrell | last post: by
3 posts views Thread by enzo | last post: by
7 posts views Thread by Ray Gardener | last post: by
3 posts views Thread by Drew McCormack | last post: by
24 posts views Thread by toton | last post: by
17 posts views Thread by Adrian Hawryluk | last post: by
6 posts views Thread by johnmmcparland | last post: by
5 posts views Thread by dragoncoder | last post: by
2 posts views Thread by snorble | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.