473,587 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.cp p

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

using namespace std;

class StringVector: public vector<void*> {
public:
void push_back(strin g* str){cout << "My_push_
back called\n";vecto r<void*>::push_ back(str);}
// following code does not work!!
string* operator[](int i) {
string* t= (string*)(vecto r<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(str 1);
s.push_back(str 2);
cout << *(string*)s[0] << endl;
cout << *(string*)s[1] << endl;
return 0;
}

Jul 19 '05 #1
5 5098

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

string* operator[](int i) {
string* t= (string*)(vecto r<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********@arc or.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.cp p

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

using namespace std;

class StringVector: public vector<void*> {
public:
void push_back(strin g* str){cout << "My_push_
back called\n";vecto r<void*>::push_ back(str);}
// following code does not work!!
string* operator[](int i) {
string* t= (string*)(vecto r<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(str 1);
s.push_back(str 2);
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_na me_here.com
Witty banter omitted for your protection
Jul 19 '05 #3

"Ulf Rimkus" <ul********@arc or.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.cp p

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

using namespace std;

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

can I do?

string* t = (string*)(*(vec tor<void*>*)thi s)[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(strin g* str){cout << "My_push_
back called\n";vecto r<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*)(*(vec tor<void*>*)thi s)[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
3626
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. --------main.cpp--------------------------------------------------------------------------- #include "AbstractActivator.h"
16
3076
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 has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
5
11323
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
5397
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 7.2 environment, the choices the optimizer makes often seem flaky. But this last example really floored me. I was hoping someone could explain why I get worse response time when the optimizer uses two indexes, than when it uses one. Some context:
2
2909
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 ("base"). I want to be able to add: 1) any derived array holding class to any other derived array holding class 2) any derived array holding class to a literal value (e.g int, double, etc) for which addition is defined for the type in the...
7
1712
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 <string> class Base { public:
2
614
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 { virtual Base& operator = (const Base &k) {} };
13
3947
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 is much appreciated. JD
17
1613
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 operator= function to copy the base classes variables in the derived class (without just copying the code?) A much simplified example: class Base {
0
7924
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
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
8219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8221
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
6629
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
5722
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
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1192
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.