« Previous entry | Next entry » Browse > Snippets
Skip to comments (8)
C++ return type overloading
Posted by Erik on Feb 01 2006 @ 16:27 :: 7839 unique visits
Normally C++ doesn't allow return type overloading. This is a simple way to get arround it.This example will show return type overloading of the * operator on vectors. This operator can be interpreted as the cross or the dot product. Normally it's enviroment will show which operation to use because the dor product results in a scalar and the cross product results in a new vector.
CODE: CPP
// very simple vector class
class vec3 {
public:
double x, y, z;
inline vec3(const double X, const double Y, const double Z) : x(X), y(Y), z(Z) {
;
}
// vector cross product
inline vec3 cross(const vec3 &a) const {
return vec3((y * a.z) - (z * a.y),
(z * a.x) - (x * a.z),
(x * a.y) - (y * a.x));
}
// vector dot product
inline double dot(const vec3 &a) const {
return ((x * a.x) + (y * a.y) + (z * a.z));
}
};
// container class which is used to do the return type overloading.
class __vec3_product {
friend __vec3_product operator * (const vec3 &l, const vec3 &r);
private:
const vec3 &l;
const vec3 &r;
// private constructor and only the * operator on vectors can create an instance (friend)
inline __vec3_product(const vec3 &L, const vec3 &R) : l(L), r(R) {
;
}
public:
// scalar, so dot
operator double () const {
return l.dot(r);
}
// vector, so cross
operator vec3 () const {
return l.cross(r);
}
};
// the * operator on 2 vectors
inline __vec3_product operator * (const vec3 &l, const vec3 &r) {
return __vec3_product(l, r);
}
class vec3 {
public:
double x, y, z;
inline vec3(const double X, const double Y, const double Z) : x(X), y(Y), z(Z) {
;
}
// vector cross product
inline vec3 cross(const vec3 &a) const {
return vec3((y * a.z) - (z * a.y),
(z * a.x) - (x * a.z),
(x * a.y) - (y * a.x));
}
// vector dot product
inline double dot(const vec3 &a) const {
return ((x * a.x) + (y * a.y) + (z * a.z));
}
};
// container class which is used to do the return type overloading.
class __vec3_product {
friend __vec3_product operator * (const vec3 &l, const vec3 &r);
private:
const vec3 &l;
const vec3 &r;
// private constructor and only the * operator on vectors can create an instance (friend)
inline __vec3_product(const vec3 &L, const vec3 &R) : l(L), r(R) {
;
}
public:
// scalar, so dot
operator double () const {
return l.dot(r);
}
// vector, so cross
operator vec3 () const {
return l.cross(r);
}
};
// the * operator on 2 vectors
inline __vec3_product operator * (const vec3 &l, const vec3 &r) {
return __vec3_product(l, r);
}
Example usage:
CODE: CPP
vec3 a(1, 2, 3);
vec3 b(4, 5, 6);
vec3 c = a * b; // will result in a cross product
double d = a * b; // ... dot product
vec3 b(4, 5, 6);
vec3 c = a * b; // will result in a cross product
double d = a * b; // ... dot product
8 comments posted so far
Add your own »
2. On Mar 13 2006 @ 14:20 guest wrote:
Great! What I was looknig for!3. On Oct 01 2008 @ 18:09 Arthur wrote:
Hey, this was what I was looking for!!!I just didn't understand anything, ahuehuahuehuahuheua
4. On Feb 03 2009 @ 07:37 TestKing wrote:
Great information thats what i was looking for some of my studies as i am also doing some of my certification of 70-502which is going to lead me in best way for my basic exams 70-656 but my main fous is on upcoming certification 70-652 which is to measured your skill for Windows Server Virtualization and Configuring.5. On Mar 17 2009 @ 11:43 guest wrote:
thanks for sharing<a href="http://www.infinitylogodesign.com/Design/Brochure-Design/">brochure designing</a> | <a href="http://www.infinitylogodesign.com/Design/Animated-Logo/">animated logos</a> | <a href="http://www.infinitylogodesign.com/">logo design</a>
6. On May 08 2009 @ 20:20 guest wrote:
http://everythingaboutalife.blogspot.com/7. On Jul 14 2009 @ 07:35 guest wrote:
AVI to DVD Converter,AVI to DVD Creator,iPhone Ringtone Maker for Mac,AVI Converter OS X,VOB Converter OS X,AVCHD Video Converter,FLV Converter,PowerPoint Converter,AVCHD Converter,Blue-Ray ripper,Rip Blue Ray,FLV to MOV Mac,VOB to DVD,HD Video Converter,iPod Playlist Transfer8. On Oct 28 2009 @ 11:13 dubbyd wrote:
This article is very interesting. Thank you very much for sharing .www.inchiriere-de-masini.ro
1. On Mar 10 2006 @ 15:01 guest wrote:
wha?