运营商存在? [英] operator presence?
问题描述
参见此示例:
class ds
{
public:
ds( ){dat [0] =''\ 0'';}
ds(int i){sprintf(dat,"%d",i);}
ds(ds& s){strcpy(dat,s.dat);}
operator int(){
int i = atoi(dat);
返回i;
}
char dat [50];
};
ds operator +(ds& a,ds& b){ds c(a); strcat(c.dat,b.dat); return c;}
ds operator +(int a,ds& b){ds c(a); strcat(ds(c).dat,b.dat);返回
c;}
ds operator +(ds& a,int b){ds c(a); strcat(c.dat,ds(b).dat);返回
c;}
int main(int char **)
{
ds a (1),b(2),c(3);
a = b + c + 7;
}
首先a + b =23。没关系。现在23+7,23和23。在添加之前,将其设置为
(int)。如何才能首先添加+(ds,int)
?
-
-Gernot
int main(int argc,char ** argv){printf
("%silto%c%cf%cgl%ssic%ccom%c"," ma" ,58,''g'',64," ba",46,10);}
________________________________________
寻找好游戏?亲自动手吧!
GLBasic - 你可以这样做
www.GLBasic .com
See this example:
class ds
{
public:
ds() {dat[0]=''\0'';}
ds(int i) {sprintf(dat, "%d", i);}
ds(ds& s) {strcpy(dat, s.dat);}
operator int(){
int i = atoi(dat);
return i;
}
char dat[50];
};
ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;}
ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return
c;}
ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return
c;}
int main(int char**)
{
ds a(1),b(2),c(3);
a = b+c+7;
}
first a+b = "23". That''s fine. Now "23"+7, the "23" get''s cased to an
(int) before adding. How can I make the adding of +(ds, int) happen
first?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
推荐答案
" Gernot Frisch" < Me@Privacy.net>在消息中写道
news:2n *********** @ uni-berlin.de ...
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2n***********@uni-berlin.de...
查看此示例:
class ds
{
公开:
ds(){dat [0] =''\ 0'';}
ds(int i){sprintf (dat,"%d",i);}
ds(ds& s){strcpy(dat,s.dat);}
operator int(){
int i = atoi(dat);
返回i;
}
char dat [50];
};
ds operator +(ds& a,ds& b){ds c(a); strcat(c.dat,b.dat);返回c;}
ds operator +(int a,ds& b){ds c(a); strcat(ds(c).dat,b.dat);返回
c;}
ds operator +(ds& a,int b){ds c(a); strcat(c.dat,ds(b).dat);返回
c;}
int main(int char **)
{a /(1),b(2),c(3); < br => a = b + c + 7;
}
首先a + b =" 23"没关系。现在23+7,23和23。在添加之前得到'
(int)。如何才能首先添加+(ds,int)?
See this example:
class ds
{
public:
ds() {dat[0]=''\0'';}
ds(int i) {sprintf(dat, "%d", i);}
ds(ds& s) {strcpy(dat, s.dat);}
operator int(){
int i = atoi(dat);
return i;
}
char dat[50];
};
ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;}
ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return
c;}
ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return
c;}
int main(int char**)
{
ds a(1),b(2),c(3);
a = b+c+7;
}
first a+b = "23". That''s fine. Now "23"+7, the "23" get''s cased to an
(int) before adding. How can I make the adding of +(ds, int) happen
first?
喜欢这个
a = b +(c + 7);
但实际上你需要在代码中添加一些常量。
ds(const ds& s){strcpy(dat,s.dat);}
运算符int()const {
ds运算符+(const ds& ; a,const ds& b){ds c(a); strcat(c.dat,b.dat);
返回c;}
等等。
如果你在整个过程中这样做,那么转换问题可能会自行解决。
(很难说,因为你正在使用只有
接近C ++的编译器)。 br />
John
Like this
a = b+(c+7);
but really you need to add some const''s to your code.
ds(const ds& s) {strcpy(dat, s.dat);}
operator int() const {
ds operator+(const ds& a, const ds& b) {ds c(a); strcat(c.dat, b.dat);
return c;}
etc. etc.
If you do this throughout then casting problem will probably solve itself.
(hard to say for certain since you are working with a compiler that only
approximates C++).
John
Gernot Frisch写道:
Gernot Frisch wrote:
class ds
{
public:
ds(){dat [0] =''\ 0'';}
ds(int i){sprintf(dat,"%d",i) ;}
ds(ds& s){strcpy(dat,s.dat);}
operator int(){
int i = atoi(dat);
返回i ;
char dat [50];
};
ds operator +(ds& a,ds& b){ds c(a); strcat(c.dat,b.dat);返回c;}
ds operator +(int a,ds& b){ds c(a); strcat(ds(c).dat,b.dat);返回
c;}
ds operator +(ds& a,int b){ds c(a); strcat(c.dat,ds(b).dat);返回
c;}
int main(int char **)
{a /(1),b(2),c(3); < br => a = b + c + 7;
}
首先a + b =" 23"没关系。现在23+7,23和23。在添加之前得到'
(int)。如何才能首先添加+(ds,int)?
class ds
{
public:
ds() {dat[0]=''\0'';}
ds(int i) {sprintf(dat, "%d", i);}
ds(ds& s) {strcpy(dat, s.dat);}
operator int(){
int i = atoi(dat);
return i;
}
char dat[50];
};
ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;}
ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return
c;}
ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return
c;}
int main(int char**)
{
ds a(1),b(2),c(3);
a = b+c+7;
}
first a+b = "23". That''s fine. Now "23"+7, the "23" get''s cased to an
(int) before adding. How can I make the adding of +(ds, int) happen
first?
制作ds&运算符const的参数。使const成为运算符
本身。
a + b的结果是临时的,暂时不能用于非
const引用是预期的。
-
Salu2
Makes the ds & paramaters of the operators const. Make const the operators
itself.
The result of a + b is a temporary, a temporary cannot be used where a non
const reference is expected.
--
Salu2
>但实际上你需要在你的代码中添加一些常量。
> but really you need to add some const''s to your code.
ds(const ds& s){strcpy(dat,s.dat);}
operator int()const {
+ const const& a,const ds& b){ds c(a); strcat(c.dat,
b.dat);返回c;}
ds(const ds& s) {strcpy(dat, s.dat);}
operator int() const {
ds operator+(const ds& a, const ds& b) {ds c(a); strcat(c.dat, b.dat); return c;}
哦,好的。我会去做的。我使用MSVC进行调试(7.1),但GCC用于
最终项目。
谢谢,
Gernot(真的绝望)
如果有人有兴趣,我希望有一个(快速)类集,允许从数字(变量+常量)到字符串和副 b
/> $ b $反之亦然,也是这样的:
cs(" A")+ some_string + 5 + cs("!")
成为:
A five 5!
Oh, OK. I''ll do that. I use MSVC for debugging (7.1), but GCC for the
final project.
Thank you,
Gernot (really in despair)
If anyone is interested, I want to have a (fast) class set that allows
casting from numbers (variables + constants) to strings and vice
versa, also alowing this:
cs("A") + some_string + 5 + cs("!")
become:
"A five 5 !"
这篇关于运营商存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!