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
14 1424
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::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.
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
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
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
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_cas t<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!"
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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);
|
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...
|
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)
|
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
| |
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 :
|
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()
|
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
{};
|
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
}
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |