Topic: C++0x Wish List


Author: "mackrune" <rune420_removethis_@start.no>
Date: Mon, 8 Jul 2002 22:40:17 GMT
Raw View
How about the ability of defining what action is to be taken on a typecast
for your class?

class A {
    int iSomeNumber;

    public:
    A(int);

    typecast(int) { return iSomeNumber; }
    template<class T> typecast(T) { return T(iSomeNumber); }
};

mackrune


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Adin Hunter Baber <mjolnir_DELETE_@soltec.net>
Date: Tue, 9 Jul 2002 08:31:54 GMT
Raw View
In article <agd22u$536$1@troll.powertech.no>,
 "mackrune" <rune420_removethis_@start.no> wrote:

> How about the ability of defining what action is to be taken on a typecast
> for your class?
>
> class A {
>     int iSomeNumber;
>
>     public:
>     A(int);
>
>     typecast(int) { return iSomeNumber; }
>     template<class T> typecast(T) { return T(iSomeNumber); }
> };
>

How is this different from using operator int ()?

class A
    {
    public:
        // ...
        operator int ()
            { return iSomeNumber; }
        // ...
    private:
        int iSomeNumber;
    };

If you don't want an implicit cast, you can make the function like this:

int asInt()
    { return iSomeNumber; }

Additionally, if you want to assign a value to iSomeNumber, you simply
have to make the return types int&.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: James Dennett <jdennett@acm.org>
Date: Tue, 9 Jul 2002 16:16:29 GMT
Raw View
mackrune wrote:
> How about the ability of defining what action is to be taken on a typecast
> for your class?
>
> class A {
>     int iSomeNumber;
>
>     public:
>     A(int);
>
>     typecast(int) { return iSomeNumber; }
>     template<class T> typecast(T) { return T(iSomeNumber); }
> };

How would this differ from the following?

struct A {
   int iSomeNumber;
   A(int);
   operator int() const { return iSomeNumber; }
   template <class T> operator T() const { return T(iSomeNumber); }
};

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]