重载铸造问题 [英] overloaded casting question

查看:85
本文介绍了重载铸造问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我只是想知道什么是重载铸造操作员的语法

是。例如,我有


struct Point {

浮动x,y,z;

};
< br $>
struct Vector {

浮动a,b,c;

};


我想要能够做类似的事情


点p;

矢量v;


((点)v ).x ++;


谢谢

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

推荐答案

laniik写道:
嗨。我只是想知道重载铸造操作员
的语法是什么。例如,我有

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

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

我希望能够做类似的事情

点p;
矢量v;

((点)v).x ++;
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++;




我强烈建议不要这样做。你真正想要的是创建一个接口来处理Vector的元素,好像它们的名字是x,y或

z。你可以为它介绍成员函数。


struct Vector {

浮动a,b,c;

浮动& x(){return a; }

};


现在写一下


vx()++;

V



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


Victor Bazarov写道:
Victor Bazarov wrote:
laniik写道:
laniik wrote:
嗨。我只是想知道重载铸造操作员
的语法是什么。例如,我有

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

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

我希望能够做类似的事情

点p;
矢量v;

((点)v).x ++;
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++;



我强烈建议反对它。你真正想要的是创建一个接口来处理Vector的元素,就好像它们有x,y或
z一样。你可以为它介绍成员函数。

结构向量{
浮动a,b,c;
浮动& x(){return a; }
};

现在写一下

vx()++;

V


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




另一种方法是从Point派生Vector.


V



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

V


Victor Bazarov写道:
Victor Bazarov wrote:
Victor Bazarov写道:
Victor Bazarov wrote:
laniik写道:
laniik wrote:
嗨。我只是想知道重载铸造操作员
的语法是什么。例如,我有

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

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

我希望能够做类似的事情

点p;
矢量v;
(点)v).x ++;
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++;



我强烈反对。你真正想要的是创建一个接口来处理Vector的元素,就好像它们有x,y或
z一样。你可以为它介绍成员函数。

结构向量{
浮动a,b,c;
浮动& x(){return a; }
};

现在写了

vx()++;

V



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



另一种方法是从Point派生Vector.

V


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

V



< ack> < GAG> < splutter>


向量是一个点吗?


< / ack> < / GAG> < / splutter>

;-)

- g

-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (12/5新帖)
http://www.cafepress。 com / goldsays


<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


这篇关于重载铸造问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆