Type conversion function for user defined type...

Discussion in 'C++' started by zaeminkr@gmail.com, May 16, 2007.

  1. Guest

    I have a question about type conversion function for user defined
    type.

    I have two classes

    class DRect
    {
    private :
    double x0;
    double y0;
    double x1;
    double y1;
    public :
    DRect(double a, double b, double c, double d) : x0(a), y0(b),
    x1(c), y1(d) {}
    void Union(DRect* p)
    {
    x0 = MIN(x0, p->x0);
    y0 = MIN(y0, p->y0);
    x1 = MAX(x1, p->x1);
    y1 = MAX(y1, p->y1);
    }
    }

    class IRect
    {
    private :
    int x0;
    int y0;
    int x1;
    int y1;
    public :
    IRect(int a, int b, int c, int d) : x0(a), y0(b), x1(c), y1(d) {}
    }


    And I want to do something like this.

    {
    DRect d(3.4, 2.6, 19.2, 93.2);
    IRect i(10, 10, 100, 100);

    d.Union(i);
    // or
    d.Union(&i)
    }


    Is it possible to make a such type conversion fuction -
    IRect::eek:perator DRect() or IRect::eek:perator DRect*()?

    Thanks in advance.
    , May 16, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    >I have a question about type conversion function for user defined
    > type.
    >
    > I have two classes
    >
    > class DRect
    > {
    > private :
    > double x0;
    > double y0;
    > double x1;
    > double y1;
    > public :
    > DRect(double a, double b, double c, double d) : x0(a), y0(b),
    > x1(c), y1(d) {}
    > void Union(DRect* p)


    You want to accept a const DRect here, as you're not interested in changing
    it (and therefore you don't need the restriction of the passed DRect being
    non-const). You probably also want to accept a reference rather than a
    pointer - passing 0 is pretty pointless (no pun intended ;)), and it makes
    the use of the class a lot easier (no need for the & everywhere, plus you
    can pass temporaries and such)


    > And I want to do something like this.
    >
    > {
    > DRect d(3.4, 2.6, 19.2, 93.2);
    > IRect i(10, 10, 100, 100);
    >
    > d.Union(i);
    > // or
    > d.Union(&i)
    > }
    >
    >
    > Is it possible to make a such type conversion fuction -
    > IRect::eek:perator DRect() or IRect::eek:perator DRect*()?


    Of course, but you obviously already know the syntax, so what's the problem?
    class IRect
    {
    // ...
    public:
    operator DRect() const { return DRect(x0, y0, x1, y1); }
    };

    Now you can use d.Union(i);

    - Sylvester
    Sylvester Hesp, May 16, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,751
    Keith Thompson
    Sep 27, 2005
  2. Kavya
    Replies:
    2
    Views:
    612
    Kavya
    Oct 31, 2006
  3. Replies:
    4
    Views:
    303
  4. Arne Mertz
    Replies:
    12
    Views:
    718
    Arne Mertz
    Apr 13, 2009
  5. Alok
    Replies:
    3
    Views:
    238
Loading...

Share This Page