Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Type conversion function for user defined type...

Reply
Thread Tools

Type conversion function for user defined type...

 
 
zaeminkr@gmail.com
Guest
Posts: n/a
 
      05-16-2007
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:perator DRect() or IRect:perator DRect*()?

Thanks in advance.

 
Reply With Quote
 
 
 
 
Sylvester Hesp
Guest
Posts: n/a
 
      05-16-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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:perator DRect() or IRect: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User defined conversion to builtin type gives l-value? Arne Mertz C++ 12 04-13-2009 01:43 PM
Type conversion function for user type again. zaeminkr@gmail.com C++ 4 05-18-2007 03:07 PM
conversion from one user defined type to another Kavya C++ 2 10-31-2006 03:50 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments