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

Overloading operator []

I have not posted to comp.lang.c++ (or comp.lang.c++.moderated)
before. In general when I have a C++ question I look for answers in
"The C++ Programming Language, Third Edition" by Stroustrup.
However, I've come upon a question that I can neither answer from
"The Book" or a Google search (so yes, at least I RTFBed). I'm
hoping that someone in this news group might know the answer.

Overloading the [] Operator

Say I want to develop a class that supports the overloaded []
operator and reads and writes the "int" type. I thought that the
way this was done was:

class MyClass
{
//...
// in theory, the RHS operator
const int operator[](const int i ) const;
// in theory, the LHS operator
int& operator[](const int i );
//...
}

Here RHS stands for right-hand-side, or an r-value and LHS stands
for left-hand-side, or an l-value.

MyClass foo;

int i = foo[j]; // RHS reference NOT!
foo[j] = i; // LHS reference

Much to my surprise, the first statement "i = foo[j];" seems to
invoke the overloaded operator I've labeled LHS. I tried this with
Microsoft's Visual C++ 6.0 compiler, I think upgraded with at least
service pack 5 (version 12.00.8804) and the GNU 2.95.2 g++ compiler
for Intel on freeBSD. Both compilers got the same results.

To put things in more concrete form, I've included a complete test
code below:

#include <stdio.h>

class overloaded
{
private:
int *pArray;

public:
overloaded( size_t size )
{
pArray = new int[ size ];
}

~overloaded()
{
delete [] pArray;
}

// in theory, the RHS operator
const int operator[](const int i ) const
{
printf("RHS a[%2d]\n", i );
return pArray[i];
}

// in theory, the LHS operator
int& operator[](const int i )
{
printf("LHS a[%2d]\n", i );
return pArray[i];
}
}; // overloaded
int
main()
{
const int len = 4;
overloaded a(len);
int b[len];

int i;

printf("initializing array...\n");
for (i = 0; i < len; i++) {
a[i] = i + 1;
}

printf("reading values from array in an 'if' statement...\n");
for (i = 0; i < len; i++) {
if (a[i] != i+1) {
printf("bad value");
break;
}
}

printf("reading values from an array in an assignment...\n");
for (i = 0; i < len; i++) {
b[i] = a[i];
}

printf("expression...\n");
int j = a[1] + a[2];
return 0;
}

When I compile and execute this code I get

initializing array...
LHS a[ 0]
LHS a[ 1]
LHS a[ 2]
LHS a[ 3]
reading values from array in an 'if' statement...
LHS a[ 0]
LHS a[ 1]
LHS a[ 2]
LHS a[ 3]
reading values from an array in an assignment...
LHS a[ 0]
LHS a[ 1]
LHS a[ 2]
LHS a[ 3]
expression...
LHS a[ 1]
LHS a[ 2]

I expected that the "initialization" would reference the LHS form of
the overloaded function. However, much to my surprise, the 'if'
statement and the value reads also referenced the LHS form of the
overloaded operator. I'm surprised at this, since as far as I can
tell, this way I've implemented the overloaded [] operators is
pretty much "text book" approach.

Is there a way to implement this class so that the RHS [] will be
called when it seems to be an r-value? That is

if (a[i] != i+1)
b[i] = a[i];
int j = a[1] + a[2];

In this example the difference is not critical, since the code gets
the expected results. However, proper invokation of the RHS and LHS
operators is important in the case of reference counted objects,
which is the appliction that originally motivated this question.

I'm working on a third version of a reference counted String class,
which can be found here:
http://www.bearcave.com/software/string/index.html. This class
suffers from a bug caused by the behavior of the [] operator
described above. In particular, it is making too many copies.

I have noted Stroustrup's solution using the Cref class (from 11.12
of "The Book"). However, in his code it appears that you might as
well omit the RHS version of the [] operator.

I'd be grateful for a version of the test code above that invokes
the RHS operator for what appear to be r-value references. Could
you please copy any postings on this to "iank at bearcave dot com".
Thank you for your help,

Ian
iank at bearcave dot com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #1
30 10366

"(null)" <ia**@idiom.com> wrote in message news:1058054598.920916@smirk...
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated)
before. In general when I have a C++ question I look for answers in
"The C++ Programming Language, Third Edition" by Stroustrup.
However, I've come upon a question that I can neither answer from
"The Book" or a Google search (so yes, at least I RTFBed). I'm
hoping that someone in this news group might know the answer.

Overloading the [] Operator

Say I want to develop a class that supports the overloaded []
operator and reads and writes the "int" type. I thought that the
way this was done was:

class MyClass
{
//...
// in theory, the RHS operator
const int operator[](const int i ) const;
// in theory, the LHS operator
int& operator[](const int i );
//...
I'd prefer

int operator[](int i ) const;
int& operator[](int i );

for an int type the other const's are unecessary.

Your comments about RHS operator and LHS operator are incorrect however. See
below.
}

Here RHS stands for right-hand-side, or an r-value and LHS stands
for left-hand-side, or an l-value.

MyClass foo;

int i = foo[j]; // RHS reference NOT!
foo[j] = i; // LHS reference

Much to my surprise, the first statement "i = foo[j];" seems to
invoke the overloaded operator I've labeled LHS. I tried this with
Microsoft's Visual C++ 6.0 compiler, I think upgraded with at least
service pack 5 (version 12.00.8804) and the GNU 2.95.2 g++ compiler
for Intel on freeBSD. Both compilers got the same results.

[snip]

I expected that the "initialization" would reference the LHS form of
the overloaded function. However, much to my surprise, the 'if'
statement and the value reads also referenced the LHS form of the
overloaded operator. I'm surprised at this, since as far as I can
tell, this way I've implemented the overloaded [] operators is
pretty much "text book" approach.

Is there a way to implement this class so that the RHS [] will be
called when it seems to be an r-value? That is

if (a[i] != i+1)
b[i] = a[i];
int j = a[1] + a[2];

It would be nice if you could do what you want to do but you can't.

For clarity lets denote operator[] by F, and operator= by G, then
essentially what you are asking for is the compiler to distinguish between
calls to F in

G(...,F(i))

from

G(F(i), ...)

But the compiler cannot do this for any normal function so there is no
reason to expect it to work for operator[] and operator=.
In this example the difference is not critical, since the code gets
the expected results. However, proper invokation of the RHS and LHS
operators is important in the case of reference counted objects,
which is the appliction that originally motivated this question.

I'm working on a third version of a reference counted String class,
which can be found here:
http://www.bearcave.com/software/string/index.html. This class
suffers from a bug caused by the behavior of the [] operator
described above. In particular, it is making too many copies.
Right, which is why you should avoid operator[] on reference counted string
classes. It is impossible to implement in an efficient manner. Implementing
operator[] on a non-const reference counted string class means a compromise,
either you have to take a copy of the string immediately even though
operator[] might only be being used to read from the string, or you have to
write a proxy class (e.g. Cref in Stroustrup).

I have noted Stroustrup's solution using the Cref class (from 11.12
of "The Book"). However, in his code it appears that you might as
well omit the RHS version of the [] operator.


Well there is no RHS version of [] you have been misinformed. If you omitted
the const version of operator[] from Stroustrup's code then the following
would not compile

const String x = "abc";
cout << x[0];

This is the true meaning of the const version of operator[], it allows you
to access const objects. Same as any other const method.

john
Jul 19 '05 #2
>
I'm working on a third version of a reference counted String class,
which can be found here:
http://www.bearcave.com/software/string/index.html. This class
suffers from a bug caused by the behavior of the [] operator
described above. In particular, it is making too many copies.


BTW, from the above site

"When an STL string object is assigned to another string object, a copy is
made. In contrast, the String container copies a reference and increments a
reference count."

This is not strictly correct, the standard leaves it up to the
implementation whether to use reference counting or not. My version of the
STL (dinkumware) does use reference counting.

john
Jul 19 '05 #3


"(null)" schrieb:
class MyClass
{
//...
// in theory, the RHS operator
const int operator[](const int i ) const;
// in theory, the LHS operator
int& operator[](const int i );
//...
}
All these members are in the private section of the class.
I assume you missed a "public:" somewhere.



Here RHS stands for right-hand-side, or an r-value and LHS stands
for left-hand-side, or an l-value.

MyClass foo;

int i = foo[j]; // RHS reference NOT!
foo[j] = i; // LHS reference

Much to my surprise, the first statement "i = foo[j];" seems to
invoke the overloaded operator I've labeled LHS.


No surprise at all.
Your foo-object is non-const, so the compiler invokes the non-const
overloaded operator[].
Why should it invoke the const version?

Things are totally different when foo is defined as const:

const MyClass foo;

Then the compiler will, of course, invoke the const-version of operator[].
Note that this overload resolution has nothing to do with the fact if you
use your overloaded operator[] at the call site as LHS or RHS.
regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #4
"John Harrison" <jo*************@hotmail.com> wrote in message news:<be************@ID-196037.news.uni-berlin.de>...
Right, which is why you should avoid operator[] on reference counted string
classes. It is impossible to implement in an efficient manner. Implementing
operator[] on a non-const reference counted string class means a compromise,
either you have to take a copy of the string immediately even though
operator[] might only be being used to read from the string, or you have to
write a proxy class (e.g. Cref in Stroustrup).

You seem to be making an assumption here. If I read your statement
correctly, you say that it is impossible to implement the non-const
index operator on a reference counted String in an efficient manner.
You then state that in order to do this, you need to write a proxy
class. Therefore, you are making the assumption that the proxy class
makes the class non-efficient.

This *may* not be true. It depends on if the compiler can optimize
away much, if not all, of the use of the proxy class.

Even if this one function makes the refernce counted String class
slightly less efficient to use, the other gains from the
refernce-counted String class *may* outweigh this small expense- it
proveably did on our system. The only way to tell on your system is
to try it.

We have a home grown reference counted String class, and it uses a
proxy class to implement the return value of the non-const index
operator. The class is extremely efficient, and fast. Our source
code base which uses this class has about 400 engineering-man-years of
development in it, consisting of hundreds of thousands of lines of
code. When we released our reference counted String class in place of
the old version, we saw at least a 10% improvement in our average CPU
consumption. Sections of our application which used Strings heavily
saw much more significant gains. We are certain that we would see
even more gains if we did not have legacy code that depended on
certain behaviors of the old String class, which required some slight
inefficiencies be purposely introduced into the new String class.

The bottom line- lots of reference-counted string bashing goes on in
this newsgroup. Most of this bashing is by people who probably never
wrote such a class themselves. We took the time to write one, and
thoroughly test and review it for completeness, correctness, and
safety. This class has had nothing but positive impact on *our*
system. Your mileage may vary.

joshua lehrer
factset research systems
NYSE:FDS

p.s. to order your very own "RefStrings Rule!" bumper-sticker, send a
self addressed stamped envelope to...
Jul 19 '05 #5

"Joshua Lehrer" <us********@lehrerfamily.com> wrote in message
news:31**************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote in message news:<be************@ID-196037.news.uni-berlin.de>...
Right, which is why you should avoid operator[] on reference counted string classes. It is impossible to implement in an efficient manner. Implementing operator[] on a non-const reference counted string class means a compromise, either you have to take a copy of the string immediately even though
operator[] might only be being used to read from the string, or you have to write a proxy class (e.g. Cref in Stroustrup).

You seem to be making an assumption here. If I read your statement
correctly, you say that it is impossible to implement the non-const
index operator on a reference counted String in an efficient manner.
You then state that in order to do this, you need to write a proxy
class. Therefore, you are making the assumption that the proxy class
makes the class non-efficient.


OK, maybe I didn't choose the best words. Substitute 'as simply as you
think' for 'efficiently'.

This *may* not be true. It depends on if the compiler can optimize
away much, if not all, of the use of the proxy class.

Even if this one function makes the refernce counted String class
slightly less efficient to use, the other gains from the
refernce-counted String class *may* outweigh this small expense- it
proveably did on our system. The only way to tell on your system is
to try it.
Couldn't disagree with that.

We have a home grown reference counted String class, and it uses a
proxy class to implement the return value of the non-const index
operator. The class is extremely efficient, and fast. Our source
code base which uses this class has about 400 engineering-man-years of
development in it, consisting of hundreds of thousands of lines of
code. When we released our reference counted String class in place of
the old version, we saw at least a 10% improvement in our average CPU
consumption. Sections of our application which used Strings heavily
saw much more significant gains. We are certain that we would see
even more gains if we did not have legacy code that depended on
certain behaviors of the old String class, which required some slight
inefficiencies be purposely introduced into the new String class.

The bottom line- lots of reference-counted string bashing goes on in
this newsgroup.
Not by me (at least not intentionally). Hell, I even believe that copy on
write is a generally useful technique.
Most of this bashing is by people who probably never
wrote such a class themselves. We took the time to write one, and
thoroughly test and review it for completeness, correctness, and
safety. This class has had nothing but positive impact on *our*
system. Your mileage may vary.

joshua lehrer
factset research systems
NYSE:FDS

p.s. to order your very own "RefStrings Rule!" bumper-sticker, send a
self addressed stamped envelope to...


Where? Where can I get one!?

john
Jul 19 '05 #6
ia**@idiom.com ((null)) writes:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated)
before. In general when I have a C++ question I look for answers in
"The C++ Programming Language, Third Edition" by Stroustrup.
However, I've come upon a question that I can neither answer from
"The Book" or a Google search (so yes, at least I RTFBed).
Well it is a big book sometimes people miss things, or don't add up
seperate things in the intended way. 10.2.6 explains constant
member functions.

I'm hoping that someone in this news group might know the answer.

Overloading the [] Operator

Say I want to develop a class that supports the overloaded []
operator and reads and writes the "int" type. I thought that the
way this was done was:

class MyClass
{
//...
// in theory, the RHS operator
const int operator[](const int i ) const;
// in theory, the LHS operator
int& operator[](const int i );
'const' is a propertery of an object, not of an operator=. If the
instance of MyClass is const, the first will be called, and if the
instance of MyClass is not const, the second will be called. This
is true regardless of which side of operator= the use of
operator[] occurs.

(Note: in order to have behavior more like that of builtin[], the
first operator[] should return 'int const&' )
//...
}

Here RHS stands for right-hand-side, or an r-value and LHS stands
for left-hand-side, or an l-value.

MyClass foo;

int i = foo[j]; // RHS reference NOT!
foo[j] = i; // LHS reference

Much to my surprise, the first statement "i = foo[j];" seems to
invoke the overloaded operator I've labeled LHS.

[snip]

This is correct behavior; foo is not const, so
int& MyClass::operator[](int) is called, and not
int const MyClass::operator[](int) const . It is constness,
and not side of operator=, that matters. Had you written:

MyClass const foo;

int i = foo[j];
foo[j]= i;

both calls would have called
int const MyClass::operator[](int) const .

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #7
us********@lehrerfamily.com (Joshua Lehrer) wrote in message
news:<31**************************@posting.google. com>...

[...]
The bottom line- lots of reference-counted string bashing goes on in
this newsgroup. Most of this bashing is by people who probably never
wrote such a class themselves. We took the time to write one, and
thoroughly test and review it for completeness, correctness, and
safety. This class has had nothing but positive impact on *our*
system. Your mileage may vary.


I suspect that most of the bashing is an accidental extension of
reference-count bashing with regards to an implementation of
std::string. The standard does NOT allow the use of a proxy -- the
non-const version of operator[] must return a real reference. This
requires special handling even in a single threaded version with
reference counting, and is almost impossible to get right efficiently in
a multi-threaded version -- you end up needing a lock for practically
every single access.

For the rest, in the past, I too have found reference counting to be a
win. In my own code -- I can't speak for others. (Like you, my string
class used a proxy as the return value for the non-const operator[].)

--
James Kanze GABI Software mailto:ka***@gabi-soft.fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
Jul 19 '05 #8
(null) <ia**@idiom.com> wrote:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated)
before. In general when I have a C++ question I look for answers in
"The C++ Programming Language, Third Edition" by Stroustrup.
However, I've come upon a question that I can neither answer from
"The Book" or a Google search (so yes, at least I RTFBed). I'm
hoping that someone in this news group might know the answer.

Overloading the [] Operator

Say I want to develop a class that supports the overloaded []
operator and reads and writes the "int" type. I thought that the
way this was done was:

class MyClass
{
//...
// in theory, the RHS operator
const int operator[](const int i ) const;
// in theory, the LHS operator
int& operator[](const int i );
//...
}

Here RHS stands for right-hand-side, or an r-value and LHS stands
for left-hand-side, or an l-value.

MyClass foo;

int i = foo[j]; // RHS reference NOT!
foo[j] = i; // LHS reference

Much to my surprise, the first statement "i = foo[j];" seems to
invoke the overloaded operator I've labeled LHS. I tried this with
Microsoft's Visual C++ 6.0 compiler, I think upgraded with at least
service pack 5 (version 12.00.8804) and the GNU 2.95.2 g++ compiler
for Intel on freeBSD. Both compilers got the same results.

To put things in more concrete form, I've included a complete test
code below:

#include <stdio.h>

class overloaded
{
private:
int *pArray; const int rhs_bracket(i) const
{
// perform = overloaded_array[i]
printf("RHS a[%2d]\n",i);
return pArray[i[;
}
void lhs_bracket(int i,int x)
{
// perform overloaded_array[i] = x;
printf("LHS a[%2d] = %d\n",i,x);
pArray[i] = x;
}
public: class proxy
{
overloaed *over;
int index;
proxy(overloaded *a,int b):over(a),index(b){}
public:
friend class overloaded;
operator int() { return over->rhs_bracket(i);}
proxy & operator = (int x)
{
over->lhs_bracket(i,x);
return *this;
}
};
friend class proxy;
proxy operator [] (int i) {return proxy(this,i);}
overloaded( size_t size )
{
pArray = new int[ size ];
}

~overloaded()
{
delete [] pArray;
}
#if 0 // in theory, the RHS operator
const int operator[](const int i ) const
{
printf("RHS a[%2d]\n", i );
return pArray[i];
}

// in theory, the LHS operator
int& operator[](const int i )
{
printf("LHS a[%2d]\n", i );
return pArray[i];
} #endif
}; // overloaded
...

This will result in different behavior depending on whether operator
[] is used to read or write data to/from an overloaded.

Note i used mutual friendship so only overloaded::operator [] will
create an overloaded::proxy objwct. and private functions to do the
different items on the lhs and rhs of an equal sign. Making them private
and proxy a friend means that only proxy is going to access these
implimentation detail functions. This approach can also be used with
operator *() and operator ->() to preform different operations when
reading and writing.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #9


James Kanze schrieb:

Normally, this should not be a problem. There are exceptions,
however, when you need to do something different if the target is
actually modified. In such cases, you typically need to use some sort
of Proxy, e.g.:

class MyClass
{
public:
class Proxy
{
friend class MyClass ;
public:
operator int() const
{
return myOwner.get( myIndex ) ;
}
Proxy const& operator=( int other ) const
{
myOwner.put( myIndex, other ) ;
return *this ;
}
private:
Proxy( MyClass& owner, int index )
: myOwner( owner )
, myIndex( index )
{
}

MyClass& myOwner ;
int myIndex ;
} ;

void put( int index, int newValue ) ;
int get( int index ) const ;

Proxy operator[]( int index )
{
return Proxy( *this, index ) ;
}
int operator[]( int index ) const
{
return get( index ) ;
}
} ;

All of the actual logic is then in get and put.


What advantage does one gain by writing a proxy class, instead of simply
returning an int&?

That is, why not simply write the following overloaded operator[]?

int& operator[](int index)
regards,

Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #10
> Much to my surprise, the first statement "i = foo[j];" seems to
invoke the overloaded operator I've labeled LHS. I tried this with
Microsoft's Visual C++ 6.0 compiler, I think upgraded with at least
service pack 5 (version 12.00.8804) and the GNU 2.95.2 g++ compiler
for Intel on freeBSD. Both compilers got the same results.


What you need is to return a proxy class that overloads operator= and
operator int. Roughly:

class MyClass
{
public:
int operator[] (int i) const; // RHS
int_proxy operator[] (int i) { return int_proxy (p_ + i); }
private:
int *p_;
};

class int_proxy
{
public:
int_proxy& operator= (int val) { *r_ = val; }
operator int () const; // RHS
int_proxy (int* r): r_ (r) { }
private:
int *r_;
};

Essentially the int_proxy acts like a int&, but whatever special code
you would have put in the RHS function in the original, you put that
same code in the RHS function in the proxy. (Stylistically, you could
use templates and put int_proxy inside of MyClass.) I believe
std::vector<bool> is specialized to do this.

Now when you call a [i], you get a temporary proxy. If there is an
access, then operator int gets called with your special RHS code; if
there is a mutate, then operator= gets called.

If it's all inline and optimizations are on, chances are the compiler
will optimize away the temporary int_proxy object. The only drawback I
can see is that the return type is no longer int&, which may crap out
code that expects it e.g. int& ri = a [i].

P.S. If you're irritated by having to repeat RHS code twice, either
cause the int_proxy version to call the MyClass version, or return a
const int_proxy from the MyClass RHS function.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #11
Rob

"(null)" <ia**@idiom.com> wrote in message news:1058054598.920916@smirk...
[Snip]

class MyClass
{
//...
// in theory, the RHS operator
const int operator[](const int i ) const;
// in theory, the LHS operator
int& operator[](const int i );
//...
}

Here RHS stands for right-hand-side, or an r-value and LHS stands
for left-hand-side, or an l-value.

MyClass foo;

int i = foo[j]; // RHS reference NOT!
foo[j] = i; // LHS reference

Much to my surprise, the first statement "i = foo[j];" seems to
invoke the overloaded operator I've labeled LHS. I tried this with
Microsoft's Visual C++ 6.0 compiler, I think upgraded with at least
service pack 5 (version 12.00.8804) and the GNU 2.95.2 g++ compiler
for Intel on freeBSD. Both compilers got the same results.

[Snip]

This is actually the way it's supposed to be. foo is not a const object,
so when
there is a choice (as in this case) the non-const version of the operator
should
be invoked.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #12
"Francis Glassborow" <fr****************@ntlworld.com> wrote in message
class hue{
public:
bool operator[](int bit)const {
if(bit <0 or bit >7) return false;
return bitset<8>(code_)[bit];
}
class ref;
friend class hue::ref;
class ref{
public:
ref(hue & h, int n):h_(h),bit(n){};
operator bool(){
if (bit <0 or bit >7) return false;
return bitset<8>(h_)[bit];
}
void operator=(bool val){
if(bit <0 or bit >7) return;
bitset<8> v(h_);
v[bit] = val;
h_ = (unsigned char)v.to_ulong();
}
private:
hue & h_;
int bit;
};
ref operator[](int bit){return ref(*this, bit);}


How do you deal with the problem of replicating the interface of class bool
in class hue::ref? No problem with class bool as you only have to worry
about operator= as you did above (and I did in my example). But what about
other member functions like operator+=, memfun(), etc?

--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #13
> That is, why not simply write the following overloaded operator[]?

int& operator[](int index)


An int& is dumb, it cannot distinguish whether it is being written to
or read. A proxy object is smart, it knows when it is written (via
operator=) or read (via operator int), and can execute different code
as a result.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #14
In message <iG*********************@bgtnsc05-news.ops.worldnet.att.net>,
Siemel Naran <Si*********@REMOVE.att.net> writes
How do you deal with the problem of replicating the interface of class bool
in class hue::ref? No problem with class bool as you only have to worry
about operator= as you did above (and I did in my example). But what about
other member functions like operator+=, memfun(), etc?


Sorry, I am totally confused. bool is a fundamental type and not a
class. Operator bool 'returns' a bool by value.

--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #15
"Francis Glassborow" <fr****************@ntlworld.com> wrote in message
Siemel Naran <Si*********@REMOVE.att.net> writes

How do you deal with the problem of replicating the interface of class boolin class hue::ref? No problem with class bool as you only have to worry
about operator= as you did above (and I did in my example). But what aboutother member functions like operator+=, memfun(), etc?


Sorry, I am totally confused. bool is a fundamental type and not a
class. Operator bool 'returns' a bool by value.


Say you have a class Foo. The operator[] that is non-const returns a proxy.
There is a function operator const Foo&() const. Then there is a function
void operator=(const Foo&). What about other member functions of class Foo?
So that we can say

EnvelopeClass env;
env["abc"]; // returns EnvelopeClass::reference, which is conceptually a
Foo&
env["abc"] = Foo(3); // ok
env["abc"].memfun(); // oops

For the last line to work, class EnvelopeClass::reference should have a
member function memfun() because class Foo has such a function. Then we
have to duplicate the entire interface of class Foo inside Foo::reference.
Quite a nuisance.

--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #16
In message <6X*********************@bgtnsc04-news.ops.worldnet.att.net>,
Siemel Naran <Si*********@REMOVE.att.net> writes
Say you have a class Foo. The operator[] that is non-const returns a proxy.
There is a function operator const Foo&() const. Then there is a function
void operator=(const Foo&). What about other member functions of class Foo?
So that we can say

EnvelopeClass env;
env["abc"]; // returns EnvelopeClass::reference, which is conceptually a
Foo&
env["abc"] = Foo(3); // ok
env["abc"].memfun(); // oops

For the last line to work, class EnvelopeClass::reference should have a
member function memfun() because class Foo has such a function. Then we
have to duplicate the entire interface of class Foo inside Foo::reference.
Quite a nuisance.


No, you are making assumptions as to what the class designer should
expose. The point of using a proxy class is exactly that it give the
class designer control whilst the return of a reference abandons it. If
we write any function that returns a plain reference to private data we
have exposed that data and lost control of it. The main motive for
having private data is exactly to avoid such circumstances. If a member
function returns a plain reference we might as well make the data
public.
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #17
Francis Glassborow wrote:
...
No, you are making assumptions as to what the class designer should
expose. The point of using a proxy class is exactly that it give the
class designer control whilst the return of a reference abandons it.
If
we write any function that returns a plain reference to private data we
have exposed that data and lost control of it.
Not true. We gave user full control of the value, which doesn't
necessarily mean that we "lost" control of it in negative sense of the
word, since the object that returned the reference might not even care
about this control.
The main motive for
having private data is exactly to avoid such circumstances. If a member
function returns a plain reference we might as well make the data
public.
...


Not true.

There still is one (although thin) level of protection that is not
destroyed by returning a reference to private data. It doesn't expose
the actual location of the lvalue, i.e. it doesn't declare loud and
clear that the returned reference is bound to a subobject of the class.
Making a subobject 'public' immediately removes this last layer of
protection.

For example, making a subobject 'public' allows user to make assumptions
about the lifetime of subobject based on the lifetime of the entire
object. Accessor member function that returns a reference prevents user
to make such assumptions.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #18
Francis Glassborow wrote:
...
No, you are making assumptions as to what the class designer should
expose. The point of using a proxy class is exactly that it give the
class designer control whilst the return of a reference abandons it.
If
we write any function that returns a plain reference to private data we
have exposed that data and lost control of it.
Not true. We gave user full control of the value, which doesn't
necessarily mean that we "lost" control of it in negative sense of the
word, since the object that returned the reference might not even care
about this control.
The main motive for
having private data is exactly to avoid such circumstances. If a member
function returns a plain reference we might as well make the data
public.
...


Not true.

There still is one (although thin) level of protection that is not
destroyed by returning a reference to private data. It doesn't expose
the actual location of the lvalue, i.e. it doesn't declare loud and
clear that the returned reference is bound to a subobject of the class.
Making a subobject 'public' immediately removes this last layer of
protection.

For example, making a subobject 'public' allows user to make assumptions
about the lifetime of subobject based on the lifetime of the entire
object. Accessor member function that returns a reference prevents user
to make such assumptions.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #19
> EnvelopeClass env;
env["abc"]; // returns EnvelopeClass::reference, which is conceptually a
Foo&
env["abc"] = Foo(3); // ok
env["abc"].memfun(); // oops

For the last line to work, class EnvelopeClass::reference should have a
member function memfun() because class Foo has such a function. Then we
have to duplicate the entire interface of class Foo inside Foo::reference.
Quite a nuisance.


At the cost of some minor ickiness you could:
1. Make the proxy overload operator*, operator-> and operator->*
(can't remember whether you can overload operator.*). So you could do
something like env["abc"]->memfun ();
2. For consistency, you could have operator= take a T*.
3. Entirely different track: both the proxy and the object could
inherit from some base class, using the "curiously recursive" pattern
to avoid virtual functions.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #20
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:<6X*********************@bgtnsc04-news.ops.worldnet.att.net>...
"Francis Glassborow" <fr****************@ntlworld.com> wrote in message
> Siemel Naran <Si*********@REMOVE.att.net> writes
> >How do you deal with the problem of replicating the interface of
> >class bool in class hue::ref? No problem with class bool as you
> >only have to worry about operator= as you did above (and I did in
> >my example). But what about other member functions like
> >operator+=, memfun(), etc?
> Sorry, I am totally confused. bool is a fundamental type and not a
> class. Operator bool 'returns' a bool by value.
Say you have a class Foo. The operator[] that is non-const returns a
proxy. There is a function operator const Foo&() const. Then there
is a function void operator=(const Foo&). What about other member
functions of class Foo? So that we can say EnvelopeClass env;
env["abc"]; // returns EnvelopeClass::reference, which is conceptually a
Foo&
env["abc"] = Foo(3); // ok
env["abc"].memfun(); // oops For the last line to work, class EnvelopeClass::reference should have
a member function memfun() because class Foo has such a function.
Then we have to duplicate the entire interface of class Foo inside
Foo::reference. Quite a nuisance.


This is a known problem. It is a major motivation behind the desire to
be able to overload operator.(). In the meantime, about the best you
can do is overload operator-> on the proxy, and require your users to
write things like:

env[ "abc" ]->memfun() ;

It exposes the fact that you are using a proxy, but until you can
overload operator.(), it is the best you can do.

--
James Kanze GABI Software mailto:ka***@gabi-soft.fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #21
Thomas Mang <a9******@unet.univie.ac.at> wrote in message
news:<3F***************@unet.univie.ac.at>...
James Kanze schrieb:
> Normally, this should not be a problem. There are exceptions,
> however, when you need to do something different if the target is
> actually modified. In such cases, you typically need to use some sort
> of Proxy, e.g.:


[Code deleted...]
What advantage does one gain by writing a proxy class, instead of
simply returning an int&? That is, why not simply write the following overloaded operator[]? int& operator[](int index)


If you can return a reference, you do. If you can't, you use a proxy.

References can be dangerous, because they allow the user direct access
to your internals. Thus, for exemple, the problemes with COW with
std::string -- if std::string::operator[] were allowed to return a
proxy, COW would be a lot easier, *and* would be a much bigger win.

Or consider a class which actually maintains the data on disk, and not
in memory. If the accesses are all through get and put (called by the
proxy), there is no problem; if you return a reference, however, you
don't know how long to lock the data in memory, and you don't know after
whether it has been modified or not, so you don't know whether you have
to write the data back or not.

--
James Kanze GABI Software mailto:ka***@gabi-soft.fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #22
In message <3F**************@hotmail.com>, Andrey Tarasevich
<an**************@hotmail.com> writes
Not true. We gave user full control of the value, which doesn't
necessarily mean that we "lost" control of it in negative sense of the
word, since the object that returned the reference might not even care
about this control.
But that is a design decision, and if you truly do not care make the
data public.
The main motive for
having private data is exactly to avoid such circumstances. If a member
function returns a plain reference we might as well make the data
public.
...


Not true.

There still is one (although thin) level of protection that is not
destroyed by returning a reference to private data. It doesn't expose
the actual location of the lvalue, i.e. it doesn't declare loud and
clear that the returned reference is bound to a subobject of the class.
Making a subobject 'public' immediately removes this last layer of
protection.

For example, making a subobject 'public' allows user to make assumptions
about the lifetime of subobject based on the lifetime of the entire
object. Accessor member function that returns a reference prevents user
to make such assumptions.


The subtle problems that this introduces are well illustrated by the
c_str() member of std::string.

Now I over stated my case because containers have internal data
structures that we do not want to expose to users, yet we do want to
provide full access to the contained objects. Generally that is easily
supplied by either references or iterators. Sometimes such as in the
case of bitset<> we cannot do that and have to provide a proxy to do the
work but that is a different design problem because the individual
elements do not have independent existence.

--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #23
Andrey Tarasevich <an**************@hotmail.com> writes:
Francis Glassborow wrote:
> ...
> No, you are making assumptions as to what the class designer should
> expose. The point of using a proxy class is exactly that it give the
> class designer control whilst the return of a reference abandons it.
> If
> we write any function that returns a plain reference to private data we
> have exposed that data and lost control of it.
Not true. We gave user full control of the value, which doesn't
necessarily mean that we "lost" control of it in negative sense of the
word, since the object that returned the reference might not even care
about this control.


This is hairsplitting on the definition of control.
> The main motive for
> having private data is exactly to avoid such circumstances. If a member
> function returns a plain reference we might as well make the data
> public.
> ...
Not true.

There still is one (although thin) level of protection that is not
destroyed by returning a reference to private data. It doesn't expose
the actual location of the lvalue,


That depends on what you mean by location. The address of the lvalue
is exposed.
i.e. it doesn't declare loud and
clear that the returned reference is bound to a subobject of the
class.
#include<iostream>

class a
{
int i;
public:
int& foo(){return i;}
};

bool is_within(void* p, void* begin, void* end)
{
return p >= begin and p < end ;
}

int main()
{
a b;
std::cout << std::boolalpha << "is_within(&b.foo(), &b, &b + 1) == "
<< is_within(&b.foo(), &b, &b + 1) << std::endl;
}
I believe the expression 'is_within(&b.foo(), &b, &b + 1)' in the
above is required to return true.
Making a subobject 'public' immediately removes this last layer of
protection.

[snip]

I used to think so too. Then I encountered a real-life situation
where someone had come to rely on what is implied by the code
above.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #24
<ka***@gabi-soft.fr> wrote in message
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message

For the last line to work, class EnvelopeClass::reference should have
a member function memfun() because class Foo has such a function.
Then we have to duplicate the entire interface of class Foo inside
Foo::reference. Quite a nuisance.


This is a known problem. It is a major motivation behind the desire to
be able to overload operator.(). In the meantime, about the best you
can do is overload operator-> on the proxy, and require your users to
write things like:

env[ "abc" ]->memfun() ;

It exposes the fact that you are using a proxy, but until you can
overload operator.(), it is the best you can do.


So essentially operator[] returns a proxy pointer. However I'm not sure if
it solves the original problem. For const member functions we just want to
forward to real function. For non-const member functions we want to call
the alter function and then forward to the real function.

class Envelope::Reference {
public:
void constmemfun() const {
return d_object.constmemfun();
}
void memfun() const {
d_envelope.alter();
return d_object.memfun();
}
private:
Envelope& d_envelope;
Envelope::Object& d_object;
};

--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #25
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:<70*********************@bgtnsc05-news.ops.worldnet.att.net>...
<ka***@gabi-soft.fr> wrote in message
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
For the last line to work, class EnvelopeClass::reference should
have a member function memfun() because class Foo has such a
function. Then we have to duplicate the entire interface of class
Foo inside Foo::reference. Quite a nuisance.
This is a known problem. It is a major motivation behind the desire
to be able to overload operator.(). In the meantime, about the best
you can do is overload operator-> on the proxy, and require your
users to write things like: env[ "abc" ]->memfun() ; It exposes the fact that you are using a proxy, but until you can
overload operator.(), it is the best you can do.
So essentially operator[] returns a proxy pointer.
Sort of a mixure. It acts like an object on the left side of
assignment, and when an lvalue to rvalue conversion is called for, but
like a pointer if you use the -> operator:-). Not an ideal situation, I
will admit.
However I'm not sure if it solves the original problem. For const
member functions we just want to forward to real function. For
non-const member functions we want to call the alter function and then
forward to the real function. class Envelope::Reference {
public:
void constmemfun() const {
return d_object.constmemfun();
}
void memfun() const {
d_envelope.alter();
return d_object.memfun();
}
private:
Envelope& d_envelope;
Envelope::Object& d_object;
};


Yet another problem.

In practice, I have only used such proxies within collections, and my
collections, like the STL, use value semantics. So you typically don't
have non-const member functions except for assignment; you also tend to
copy the object you're interested in out of the collection, and work
with the copy, potentially copying it back in.

I suppose if I had to deal with objects with behavior, I'd arrange for
operator[] (or the operator-> of the proxy) to return some sort of smart
pointer -- if the collection is caching objects, for example, you'd need
the smart pointer to tell you when you could free the object, for
example.

--
James Kanze GABI Software mailto:ka***@gabi-soft.fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #26
llewelly wrote:
>
> There still is one (although thin) level of protection that is not
> destroyed by returning a reference to private data. It doesn't expose
> the actual location of the lvalue,


That depends on what you mean by location. The address of the lvalue
is exposed.


I meant a completely different notion of location (OK, I agree that
"location" is rather bad choice of word). What I meant is explained
below (after "i.e."):
> i.e. it doesn't declare loud and
> clear that the returned reference is bound to a subobject of the
> class.


#include<iostream>

class a
{
int i;
public:
int& foo(){return i;}
};

bool is_within(void* p, void* begin, void* end)
{
return p >= begin and p < end ;
}

int main()
{
a b;
std::cout << std::boolalpha << "is_within(&b.foo(), &b, &b + 1) == "
<< is_within(&b.foo(), &b, &b + 1) << std::endl;
}

I believe the expression 'is_within(&b.foo(), &b, &b + 1)' in the
above is required to return true.


Yes, in this particular case it is required to return 'true'. (Actually,
reading 5.9/2 I can't immediately say whether the comparison between
'&b.foo()' and '&b + 1' is specified by the standard. But let's assume
that it is.) But this technique cannot legally be used to detect the
situation when the returned reference is bound to a subobject of given
object. The problem arises when the returned reference is _not_ bound to
a subobject. In this case the pointer comparison in the above
'is_within' function is unspecified (see 5.9/2), which means that
basically anything can be returned. In other words, when this function
returns 'true', it means one of two things:

1) pointer 'p' points to some location between 'begin' and 'end'
2) the comparison is unspecified and the returned result is nothing but
an accident - a particular case of unspecified result, that just happens
to be 'true' this time

Can you come up with a way to differentiate these two situations? I
don't believe it is possible.
> Making a subobject 'public' immediately removes this last layer of
> protection.

[snip]

I used to think so too. Then I encountered a real-life situation
where someone had come to rely on what is implied by the code
above.
...


As I said above, this code does not illustrate any legal technique,
which means that I can't accept it as an argument.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #27
Andrey Tarasevich <an**************@hotmail.com> writes:
llewelly wrote:
> >
> > There still is one (although thin) level of protection that is not
> > destroyed by returning a reference to private data. It doesn't expose
> > the actual location of the lvalue,

>
> That depends on what you mean by location. The address of the lvalue
> is exposed.


I meant a completely different notion of location (OK, I agree that
"location" is rather bad choice of word). What I meant is explained
below (after "i.e."):
> > i.e. it doesn't declare loud and
> > clear that the returned reference is bound to a subobject of the
> > class.

>
> #include<iostream>
>
> class a
> {
> int i;
> public:
> int& foo(){return i;}
> };
>
> bool is_within(void* p, void* begin, void* end)
> {
> return p >= begin and p < end ;
> }
>
> int main()
> {
> a b;
> std::cout << std::boolalpha << "is_within(&b.foo(), &b, &b + 1) == "
> << is_within(&b.foo(), &b, &b + 1) << std::endl;
> }
>
> I believe the expression 'is_within(&b.foo(), &b, &b + 1)' in the
> above is required to return true.


Yes, in this particular case it is required to return 'true'. (Actually,
reading 5.9/2 I can't immediately say whether the comparison between
'&b.foo()' and '&b + 1' is specified by the standard. But let's assume
that it is.) But this technique cannot legally be used to detect the
situation when the returned reference is bound to a subobject of given
object. The problem arises when the returned reference is _not_ bound to
a subobject. In this case the pointer comparison in the above
'is_within' function is unspecified (see 5.9/2), which means that
basically anything can be returned. In other words, when this function
returns 'true', it means one of two things:

1) pointer 'p' points to some location between 'begin' and 'end'
2) the comparison is unspecified and the returned result is nothing but
an accident - a particular case of unspecified result, that just happens
to be 'true' this time

[snip]

This is quite similar to my response the first time I encountered this
issue. What I didn't realize is that most real programs must rely
on on some unspecified, undefined, or implementation-defined
behavior (though such code is hopefully labled, conditioned
according to platform, and most importantly confined), and in
certain areas (e.g., implementing garbage collectors) relying on
pointer comparisons is common.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #28
> No, you are making assumptions as to what the class designer should
expose. The point of using a proxy class is exactly that it give the
class designer control whilst the return of a reference abandons it. If
we write any function that returns a plain reference to private data we
have exposed that data and lost control of it. The main motive for
having private data is exactly to avoid such circumstances. If a member
function returns a plain reference we might as well make the data
public.


In general I agree with you and disagree with the parent post -- it is
dangerous to return references. However there is one other case I can
think of besides containers where you may want to return references:
where you want to restrict to const access.

E.g.
class X
{
private:
Y y_;
public:
const Y& getReference () const { return y_; }
};

Here you don't want to expose the data member y_ to arbritary change,
you want all clients (even those with a non-const reference to an X)
to only be able to see y_ and not change it.

The moral equivalent of:

class X
{
private:
Y y_;
public:
const Y& reference;
X (...): y_ (...), reference (y_) { }
};

.... though I'm not sure by C++ standard rules whether y_ exists at the
right time to get a reference to it.

Cheers,
Glen Low
http://www.pixelglow.com/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #29
Hi,

Glen Low wrote:
The moral equivalent of:

class X
{
private:
Y y_;
public:
const Y& reference;
X (...): y_ (...), reference (y_) { }
};

... though I'm not sure by C++ standard rules whether y_ exists at the
right time to get a reference to it.


Members are initialized in the order of their declaration, independent
of their order in the initializer list.

In other words, 'y_' is initialized before 'reference', so 'reference'
is initialized with a reference to the already initialized 'y_'.

Interestingly:

class X
{
public:
const Y& reference;
X (...): y_ (...), reference (y_) { }
private:
Y y_;
};

Here, 'reference' is initialized with a reference to something that has
not yet been initialized, but that already has memory allocated. This is
a problem, and I cannot find in the Standard any clear statement that
would either bless or damn it. Some compilers accept this code. For
sure, *using* a reference to something that does not yet exist is forbidden.

--
Maciej Sobczak
http://www.maciejsobczak.com/

Distributed programming lib for C, C++, Python & Tcl:
http://www.maciejsobczak.com/prog/yami/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #30
Hello, Maciej Sobczak!

Maciej Sobczak schrieb:
[snip]
Interestingly:

class X
{
public:
const Y& reference;
X (...): y_ (...), reference (y_) { }
private:
Y y_;
};

Here, 'reference' is initialized with a reference to something that has
not yet been initialized, but that already has memory allocated. This is
a problem, and I cannot find in the Standard any clear statement that
would either bless or damn it. Some compilers accept this code. For
sure, *using* a reference to something that does not yet exist is forbidden.


My interpretation of what is written in 3.7.3.1/p. 2 and in 3.8/p. 5 let me
deduce, that your example should be well-defined.

Greetings,

Daniel


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #31

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

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
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...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
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,...
0
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...

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.