473,406 Members | 2,293 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,406 software developers and data experts.

Type conversion function for user defined type...

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::operator DRect() or IRect::operator DRect*()?

Thanks in advance.

May 16 '07 #1
1 1997

<za******@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr 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::operator DRect() or IRect::operator 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
May 16 '07 #2

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

Similar topics

16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
4
by: Alex Page | last post by:
This is probably really basic, but I can't seem to get it to work. I'm trying to create an enumerated type, using the following code: CREATE FUNCTION enum_gender_in (cstring) RETURNS enum_gender...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
3
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
6
by: cppnow | last post by:
Hello. I have a strange conceptual problem I'm trying to think about. I would like to build a library that allows the user to do the following: 1) User defined types: The user defines their...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.