473,466 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Create 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 1384
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::normalize(v);

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

Vector::normalize((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::normalize(v);

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

Vector::normalize((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(Vector 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::normalize(v);

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

Vector::normalize((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
lilburne wrote:
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::normalize(v);

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

Vector::normalize((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.


For the initial example given by the OP, we can keep Vector and Point
classes as separate types, whilst still avoiding the need for casts, by
having them implement an interface.

That is, both class can inherit a pure abstract class.

class ICoordinate {
public:

double x() = 0;
double y() = 0;
double z() = 0;

}

class Point : public ICoordinate {
public:

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

class Vector : public ICoordinate {
public:

double x() { return i(); )
double y() { return j(); }
double z() { return K(); }

double i();
double j();
double k();
Point anchor();
};

The code that would have needed to cast a Vector to a Point, would
actually use a pointer/reference of type ICoordinate, and simply call
the appropriate interface method.
It doesn't address how we access Point or Vector specific methods like
Point::anchor() or Vector::release(), but these methods are not the same
so it would not be appropriate to use an Interface for them.

Andrew
Jul 23 '05 #11
hm ok thanks for the advice. i will reconsider implementing what was
originally just an attempt to save three lines of code and a little
curosity.

so now I have a new (totally unrelated) question:
say have 2 classes

struct BLAHG {
float erg,gha,moo;
}

struct OOGA {
float roo, hak, mup;
}

and i want to do a cast

OOGA o;
BLAHG b;

(BLAHG)o

where it maps the respective internal elements.

how would i do that? : P

Jul 23 '05 #12
laniik wrote:
hm ok thanks for the advice. i will reconsider implementing what was
originally just an attempt to save three lines of code
I think this may be the cause of your problem. You may be removing the 3
lines of code within the classes, but how many lines of cast code would
you be implementing.

Don't think about saving three lines of code, its better to think about
the design. Once you do this, any duplication (in this case the
Casting) would be removed anyway.

and a little curosity.

so now I have a new (totally unrelated) question:
From a design point of view, this appears to be the same question.
say have 2 classes

struct BLAHG {
float erg,gha,moo;
}

struct OOGA {
float roo, hak, mup;
}

and i want to do a cast

OOGA o;
BLAHG b;

(BLAHG)o

where it maps the respective internal elements.

how would i do that? : P

Jul 23 '05 #13
laniik wrote:
hm ok thanks for the advice. i will reconsider implementing what was
originally just an attempt to save three lines of code and a little
curosity.

so now I have a new (totally unrelated) question:
say have 2 classes

struct BLAHG {
float erg,gha,moo;
}

struct OOGA {
float roo, hak, mup;
}

and i want to do a cast

OOGA o;
BLAHG b;

(BLAHG)o

where it maps the respective internal elements.

how would i do that? : P


You could use reintpret_cast<T*> or reinterpret_cast<T&> or
and old style C cast. But you really don't want to do that.
The problem is that over time BLAGH nearly always turns into
BLAGH1 and OOGA nearly always turns into OOGA1. As the old
80s saying went:

"The world could end due to a misplaced typecast!"
Jul 23 '05 #14
Andrew McDonagh wrote:


It doesn't address how we access Point or Vector specific methods like
Point::anchor() or Vector::release(), but these methods are not the same
so it would not be appropriate to use an Interface for them.


I think that is exactly what the OP wants to do. He wants to
perform operations from disparate classes based on
commonality of implementation details.

Jul 23 '05 #15

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

Similar topics

1
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...
1
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...
1
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...
30
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...
6
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...
20
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...
10
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...
1
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
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
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,...
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
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.