473,785 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloaded casting question

Hi. Im just wondering what the syntax to overload a casting operator
is. For example, i have

struct Point {
float x,y,z;
};

struct Vector {
float a,b,c;
};

and i want to be able to do somthing like

Point p;
Vector v;

((Point)v).x++;

thanks

Jul 23 '05 #1
14 1422
laniik wrote:
Hi. Im just wondering what the syntax to overload a casting operator
is. For example, i have

struct Point {
float x,y,z;
};

struct Vector {
float a,b,c;
};

and i want to be able to do somthing like

Point p;
Vector v;

((Point)v).x++;


I would strongly advise against it. What you really want is to create
an interface to treat the Vector's elements as if they had names x, y, or
z. You can introduce member functions for that.

struct Vector {
float a,b,c;
float& x() { return a; }
};

Now write

v.x()++;

V
Jul 23 '05 #2
Victor Bazarov wrote:
laniik wrote:
Hi. Im just wondering what the syntax to overload a casting operator
is. For example, i have

struct Point {
float x,y,z;
};

struct Vector {
float a,b,c;
};

and i want to be able to do somthing like

Point p;
Vector v;

((Point)v).x++;

I would strongly advise against it. What you really want is to create
an interface to treat the Vector's elements as if they had names x, y, or
z. You can introduce member functions for that.

struct Vector {
float a,b,c;
float& x() { return a; }
};

Now write

v.x()++;

V


Another way is to derive Vector from Point, of course.

V
Jul 23 '05 #3
Victor Bazarov wrote:
Victor Bazarov wrote:
laniik wrote:
Hi. Im just wondering what the syntax to overload a casting operator
is. For example, i have

struct Point {
float x,y,z;
};

struct Vector {
float a,b,c;
};

and i want to be able to do somthing like

Point p;
Vector v;

((Point)v).x++;


I would strongly advise against it. What you really want is to create
an interface to treat the Vector's elements as if they had names x, y, or
z. You can introduce member functions for that.

struct Vector {
float a,b,c;
float& x() { return a; }
};

Now write

v.x()++;

V

Another way is to derive Vector from Point, of course.

V

<ack> <gag> <splutter>

Is a Vector a Point?

</ack> </gag> </splutter>
;-)

--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #4
hm. just out of curosity, why do you advise against it. Really, the
reason is not so i can rename the variables, that was just an example
of what they do. The reason i would want to do that is because i often
find myself wanting to perform vector functions on points:

example.

Vector v;
Point p;

v.a=p.x;
v.b=p.y;
v.c=p.z;

Vector::normali ze(v);

where really i would like to remove the need of those 3 lines, and have

Vector::normali ze((Vector)p);

for example.

thanks
oliver

Jul 23 '05 #5
a vector is not a point, of course, but they are similar in terms of
local data, and im often wanting to perform vector operations on a
vector that woul have the same values as a specific point.

Jul 23 '05 #6
also,

(regardless of the correctness of vectors/points) im also curious how
its done!

: )

Jul 23 '05 #7
Victor Bazarov wrote:

Now write

v.x()++;

V

Another way is to derive Vector from Point, of course.

I don't think our mathematicians would agree, in fact I can
point to 10 years of interminable arguments about a point
isn't a vector, etc, etc.

class Point {
public:

double x();
double y();
double z();
Vector release();
};

class Vector {
public:

double i();
double j();
double k();
Point anchor();
};
Jul 23 '05 #8
laniik wrote:
hm. just out of curosity, why do you advise against it.
You're trying to introduce a tight coupling where none seem to exist.
If you know that it does in fact exist, there are better ways to put it
into C++ terms. See Inheritance, Containment, Implementation in terms of
and so on...
Really, the
reason is not so i can rename the variables, that was just an example
of what they do. The reason i would want to do that is because i often
find myself wanting to perform vector functions on points:

example.

Vector v;
Point p;

v.a=p.x;
v.b=p.y;
v.c=p.z;

Vector::normali ze(v);

where really i would like to remove the need of those 3 lines, and have

Vector::normali ze((Vector)p);

for example.


What does it mean to normalize a point if that point is not a vector?
What you need, probably is a way to convert one into the other. Define
respective parameterized constructors in each class.

class Point;

struct Vector {
float a,b,c;
Vector(Point const &p);
};

struct Point {
float x,y,z;
Point(Vector const &v);
};

Vector::Vector( Point const &p) : a(p.x), b(p.y), c(p.z) {}
Point::Point(Ve ctor const &v) : x(v.a), y(v.b), z(v.c) {}
V
Jul 23 '05 #9
laniik wrote:
hm. just out of curosity, why do you advise against it. Really, the
reason is not so i can rename the variables, that was just an example
of what they do. The reason i would want to do that is because i often
find myself wanting to perform vector functions on points:

example.

Vector v;
Point p;

v.a=p.x;
v.b=p.y;
v.c=p.z;

Vector::normali ze(v);

where really i would like to remove the need of those 3 lines, and have

Vector::normali ze((Vector)p);

for example.


Points and vectors are our stock in trade. Most of the time
we don't have any real problems with keeping them seperate
and converting a Point to a Vector and back again as needed,
besides our vector class has a cached length too. Heck we
even have a UnitVector class too. For us the important thing
is clarity in the code, and a minimal use of casting. If we
really need to get jiggywithit these things get converted to
raw arrays.

Jul 23 '05 #10

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

Similar topics

1
2226
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen pertaining to casting class A to class B, the implementation of the
1
1635
by: Dave Corby | last post by:
Hi all, I have an overloaded template function, and in one particular spot can't get the right version of it to be called. Everywhere else in the program the correct version is called. Here's the function declarations: template <class NumT, class ExpT> NumT rppower (NumT const x, ExpT const y); template <class NumT, class ExpT> SafeInt<NumT> rppower (SafeInt<NumT> const x, SafeInt<ExpT> const y);
1
10010
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type A, and the second one of type B. Let say, these are not my classes and I know nothing about the implementation. As system doesn't know what code must be used for resolving the language construction a+b (where A a; and B b;), it returns "The call...
30
1741
by: Philippe Bertrand | last post by:
Is this a bug in the C# compiler or CLR runtime? enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 } class Foo { public Foo(string,object) { ... } public Foo(string,MyEnum) { ... } } Foo f = new Foo("", 0); // Uses Foo(string,MyEnum) constructor instead of Foo(string,object)
6
1310
by: Joe HM | last post by:
Hello - The following code will crash if the TestA() instances are declared as Private and work just fine if they are Public. Is there a way to hide these instances to callers from outside the Module? It should only be possible to call the one instance of TestA() without the second argument. ModuleX
20
4961
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got a VIRTUAL function createList(). This createList() function is overloaded in another class named ct_server that inherits gs_object. in my code, it looks something like that :
10
2628
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name "input$char$void" int input(); //can be compiled to name "input$int$void" .... int i= 3+'0'+input(); //can be compiled to: int(3)+int('0')+input$int$void()
1
1497
by: joseph cook | last post by:
I'm confused by this output. I would expect the more specific overloaded function to be selected: #include <iostream> class Base { }; class Derived : public Base {};
2
3889
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.