Topic: overload of operator


Author: James Kuyper <kuyper@wizard.net>
Date: 1998/04/20
Raw View
X. Liu wrote:
>
> If overload of operator to pointer is not permited, there is no way to
> direct use sort for containers of pointers to objects in STL. The sort is
> applied in STL as:
>
> template<class It>
> void sort(It first, It last);
>
> and the default operator < for (*It) is used to compare the object in
> container. I wonder there is any way to solve the problem. For example:

There is an overloaded sort() function which takes a third argument,
which is a function object used to compare two values. That object's
operator() can de-reference the two pointers, and do anything you want
with the corresponding members.
You'll need to figure out function objects, however. They look fun, and
the standard library provides a lot of useful support, but there's still
plenty of room for confusion.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/04/16
Raw View
X. Liu wrote:
>
> If overload of operator to pointer is not permited, there is no way to
> direct use sort for containers of pointers to objects in STL.

Right, you can't do that. But you wouldn't want to anyway, because then
you could never iterate over a built-in array or new'ed array of those
objects, because the pointer comparison would be broken.

> // Now I want to sort the objects pointers in the container according
> // the operators I defined, must I write the sorting code myself?

It's easier than that. Just use the version of sort() that allows you to
provide your own comparator:

  template<class RandomAccessIterator, class Compare>
    void sort(RandomAccessIterator first, RandomAccessIterator last,
              Compare comp);

The 'comp' argument is a comparison functor (or, I believe a pointer to
comparison function). Write the comparator so that it compares the
pointed-to objects rather than the pointers.

Another solution would be to define a special managed pointer type that
provides the comparison semantics you want. That takes care of both the
lifetime management and comparison rules, at the expense of more code.
--
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds

My reply address is correct as-is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "X. Liu" <liu@erdw.ethz.ch>
Date: 1998/04/10
Raw View
Hi,

I am wondering how standard restrict the overload of operators. It is
reasonable to forbid the operator overload to primitive types (for example
int operator + (int, int) ). However When I use VC++(5.0 plus sever pack 3),
I find VC++ even forbid the operators for pointers to class.

class MyObj{
//...........
};

bool operator<(const MyObj *a, const MyObj *b)  // compiler generator error
here
{
//.........
return true;
}

The above code can not be compiled by VC++. In this aspect VC++ is conform
to the standard or not? Anyone gives some comments?

Xingzeng




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/04/10
Raw View
X. Liu wrote:
>
> I am wondering how standard restrict the overload of operators. It is
> reasonable to forbid the operator overload to primitive types (for
> example int operator + (int, int) ). However When I use VC++(5.0 plus
> sever pack 3), I find VC++ even forbid the operators for pointers to
> class.

> bool operator<(const MyObj *a, const MyObj *b)  // error
>
> The above code can not be compiled by VC++. In this aspect VC++ is
> conform to the standard or not? Anyone gives some comments?

VC++ is right. In short, ALL pointer types are basic types, even
pointers to class. Even though MyObj is not a basic type, MyObj* is.
--
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds

My reply address is correct as-is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/04/12
Raw View
In article 1@elna.ethz.ch, "X. Liu" <liu@erdw.ethz.ch> writes:
>
>I am wondering how standard restrict the overload of operators. It is
>reasonable to forbid the operator overload to primitive types (for example
>int operator + (int, int) ). However When I use VC++(5.0 plus sever pack 3),
>I find VC++ even forbid the operators for pointers to class.

The compiler is correct.

It was always the rule in C++ that at least one argument of an overloaded
operator must be of a class type or a reference to a class type. (If the
function is a class member function, that meets the requirement.) See the
ARM, p 330.

The draft standard relaxes the rule, and allows overloaded operators
on enum (and reference to enum) types as well.

Pointer types do not fulfill the requirements for arguments of overloaded
operators.

Reference: Draft standard 13.5 "Overloaded operators", paragraph 6.

---
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Mike Schilling <mikes@forte.com>
Date: 1998/04/13
Raw View
Steve Clamage wrote:
>
> In article 1@elna.ethz.ch, "X. Liu" <liu@erdw.ethz.ch> writes:
> >
> >I am wondering how standard restrict the overload of operators. It is
> >reasonable to forbid the operator overload to primitive types (for example
> >int operator + (int, int) ). However When I use VC++(5.0 plus sever pack 3),
> >I find VC++ even forbid the operators for pointers to class.
>
> The compiler is correct.
>
> It was always the rule in C++ that at least one argument of an overloaded
> operator must be of a class type or a reference to a class type. (If the
> function is a class member function, that meets the requirement.) See the
> ARM, p 330.
>
> The draft standard relaxes the rule, and allows overloaded operators
> on enum (and reference to enum) types as well.

Minor quibble.  My edition of the ARM (with corrections, April 1994)
also allows overloadings for enums and references thereto.

Mike



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "X. Liu" <liu@erdw.ethz.ch>
Date: 1998/04/14
Raw View
If overload of operator to pointer is not permited, there is no way to
direct use sort for containers of pointers to objects in STL. The sort is
applied in STL as:

template<class It>
void sort(It first, It last);

and the default operator < for (*It) is used to compare the object in
container. I wonder there is any way to solve the problem. For example:

bool operator<(const Shape &a, const Shape &)
{
  //...........
  return true;
}
typedef vector<Shape *> ShapeContainer;
ShapeContainer shapes;
shapes.push_back(new Shape);
//.................
// Now I want to sort the objects pointers in the container according the
operators I defined
// must I write the sorting code myself?

Xingzeng

>VC++ is right. In short, ALL pointer types are basic types, even
>pointers to class. Even though MyObj is not a basic type, MyObj* is.
>--
>Bradd W. Szonye
>bradds@concentric.net
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]