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;
} 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];
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
"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
> 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
};
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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"
|
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.
|
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)
|
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:
|
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...
| |
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:
|
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) {}
};
|
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
|
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
{
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |