473,396 Members | 1,929 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,396 software developers and data experts.

How to access operator from base class?

Hallo!

Now I've tried out for quite a long time how to access the []-operator from
a base class. But I cannot find out how to do this. Well, it's an example
from "Think in C++".
So, I guess I need some help from any real C++ programmer.

Greetings, Ulf

// 14.9 Inherit a class StringVector from vector<void*> and redefine the
push_back( ) and operator[] member
// functions to accept and produce string*. What happens if you try to
push_back( ) a void*?

//StringVector.cpp

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

using namespace std;

class StringVector: public vector<void*> {
public:
void push_back(string* str){cout << "My_push_
back called\n";vector<void*>::push_back(str);}
// following code does not work!!
string* operator[](int i) {
string* t= (string*)(vector<void*>&)this[i]; //wrong! What can I do?
return (t);
}
};

int
main(){
StringVector s;
string *str1 = new string("Hallo");
string *str2 = new string("Welt");
s.push_back(str1);
s.push_back(str2);
cout << *(string*)s[0] << endl;
cout << *(string*)s[1] << endl;
return 0;
}

Jul 19 '05 #1
5 5084

"Ulf Rimkus" <ul********@arcor.de> wrote in message news:3f**********************@newsread2.arcor-online.net...
Hallo!

string* operator[](int i) {
string* t= (string*)(vector<void*>&)this[i]; //wrong! What can I do?


You're applying the vector& cast to the return of this[i] (which is recursive). You want to apply it
to "this". You need more parens.
(string*) ((vector<void*>&)this)[i];

Jul 19 '05 #2
On Mon, 14 Jul 2003 17:09:59 +0200, Ulf Rimkus <ul********@arcor.de>
wrote:
Hallo!

Now I've tried out for quite a long time how to access the []-operator from
a base class. But I cannot find out how to do this. Well, it's an example
from "Think in C++".
So, I guess I need some help from any real C++ programmer.

Greetings, Ulf

// 14.9 Inherit a class StringVector from vector<void*> and redefine the
push_back( ) and operator[] member
// functions to accept and produce string*. What happens if you try to
push_back( ) a void*?

//StringVector.cpp

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

using namespace std;

class StringVector: public vector<void*> {
public:
void push_back(string* str){cout << "My_push_
back called\n";vector<void*>::push_back(str);}
// following code does not work!!
string* operator[](int i) {
string* t= (string*)(vector<void*>&)this[i]; //wrong! What can I do?
return (t);
}
};

int
main(){
StringVector s;
string *str1 = new string("Hallo");
string *str2 = new string("Welt");
s.push_back(str1);
s.push_back(str2);
cout << *(string*)s[0] << endl;
cout << *(string*)s[1] << endl;
return 0;
}

In general:

(*this)[]

or, if you like:

this->operator[]

or simply :

operator[]

Will do.

</dib>

John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection
Jul 19 '05 #3

"Ulf Rimkus" <ul********@arcor.de> wrote in message
news:3f**********************@newsread2.arcor-online.net...
Hallo!

Now I've tried out for quite a long time how to access the []-operator from a base class. But I cannot find out how to do this. Well, it's an example
from "Think in C++".
So, I guess I need some help from any real C++ programmer.

Greetings, Ulf

// 14.9 Inherit a class StringVector from vector<void*> and redefine the
push_back( ) and operator[] member
// functions to accept and produce string*. What happens if you try to
push_back( ) a void*?

//StringVector.cpp

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

using namespace std;

class StringVector: public vector<void*> {
public:
void push_back(string* str){cout << "My_push_
back called\n";vector<void*>::push_back(str);}
// following code does not work!!
string* operator[](int i) {
string* t= (string*)(vector<void*>&)this[i]; //wrong! What

can I do?

string* t = (string*)(*(vector<void*>*)this)[i];

I have my doubts about the whole concept (deriving from std::vector is not
recommended) but at least that should compile.

john
Jul 19 '05 #4
> Now I've tried out for quite a long time how to access the []-operator from
a base class. But I cannot find out how to do this. Well, it's an example
from "Think in C++".
So, I guess I need some help from any real C++ programmer.
class StringVector: public vector<void*> {
public:
void push_back(string* str){cout << "My_push_
back called\n";vector<void*>::push_back(str);}


The danger of publicly inheriting from vector<void*> is that
operator[] is not virtual in the vector class. So if you pass a
StringVector to a function that expects a vector<void*>, the compiler
will happily accept it, but if that function then calls operator[], it
will call the original vector<void*> operator not the one you've just
created.

The easiest (and slightly safer) way to do what you want to do is to
privately inherit from vector<void*>, then selectly bring back the
member functions that you want and redefine the ones you want to
redefine e.g.

class StringVector: private vector<void*> {
public:
using vector<void*>::size;
// etc.
void push_back (string* str); // redefine as the others have
suggested
};
Jul 19 '05 #5
John Harrison wrote:

string* t = (string*)(*(vector<void*>*)this)[i];

I have my doubts about the whole concept (deriving from std::vector is not
recommended) but at least that should compile.

john


Hallo John!

Thank you! This one compiled and did what I intented it should do. Now I
try to really understand this line of code.

Ulf

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Boogie El Aceitoso | last post by:
Hi, The code below produces an access violation error, complaining that an object is destroyed twice. I don't understand why this happens. Any help would be appreciated. ...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
5
by: Bob Hairgrove | last post by:
Consider the following: #include <string> class A { public: A( const std::string & full_name , const std::string & display_name) : m_full_name(full_name)
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
7
by: Jim Langston | last post by:
This is something someone was asking in irc. I really don't need to do this right now, but may have to in the future. The following code is in error (marked). #include <iostream> #include...
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
17
by: Corey Cooper | last post by:
I have a class for which I needed to create an operator= function. I then use that as a base class for another class, which also needs an operator=. What is the syntax to use in the derived class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.