473,224 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 1537

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
3
by: enzo | last post by:
hi all! i need in my libraries a class to deal with messages. I did: class messenger { public: messenger(const char* p, ostream& p_s) : m(p), m_stream(p_s)
7
by: Ray Gardener | last post by:
C++ doesn't allow: class counter : public int {}; Was there any special reason why base class ids can only be struct/class ids? This limitation appears to violate the concept of uniform...
3
by: Drew McCormack | last post by:
Does operator() get inherited? I would have thought so, but I can't get the following to work: I have a base class that takes its derived class as a template template parameter (the ...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
6
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. class Person has the << operator; /**
5
by: dragoncoder | last post by:
I got this code from a friend of mine. #include <iostream> using namespace std; class Base { int i; public:
2
by: snorble | last post by:
I started creating a simple "bits" class, intended to act like a array of bits. This was my initial idea, basically just overriding the string representation to display the bitmask (so far): ...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.