473,796 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.l earn.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 1560

"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in
message news:5P******** ***********@new ssvr16.news.pro digy.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******** ***********@new ssvr16.news.pro digy.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<Fie ld> fields;
public:
Record()
{
fields.push_bac k(Field("one")) ;
fields.push_bac k(Field("two")) ;
fields.push_bac k(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******** ***********@new ssvr16.news.pro digy.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_Recor d 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_Recor d:

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::operato r[] 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.l earn.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******** ***********@new ssvr16.news.pro digy.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_Recor d 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_Recor d:

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<Fie ld> fields;
public:
Record()
{
fields.push_bac k(Field("one")) ;
fields.push_bac k(Field("two")) ;
fields.push_bac k(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.l earn.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
6889
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: // TestA.h --------------------------------------- #include <iostream> enum Aval { FIRST_VALUE,
3
2815
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
1424
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 type conceptualization; i.e. classes can never have or extend an is-a relationship to a scalar.
3
1706
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 Barton-Nackmann trick). It has a operator() defined. template <typename NumType, template <typename T> class DerivedType> class Matrix { public:
24
2962
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
17
2450
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 and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
6
1620
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
1355
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
1703
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): class bits(long): def __str__ (self): s = '' if self == 0L: s += '-' else:
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.