运营商存在?

扫码查看
本文介绍了运营商存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 参见此示例: 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 .comSee 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); returnc;}ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); returnc;}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) happenfirst?---Gernotint 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推荐答案 喜欢这个 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 /> JohnLike thisa = 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 onlyapproximates C++).John 制作ds&运算符const的参数。使const成为运算符 本身。 a + b的结果是临时的,暂时不能用于非 const引用是预期的。 - Salu2Makes the ds & paramaters of the operators const. Make const the operatorsitself.The result of a + b is a temporary, a temporary cannot be used where a nonconst reference is expected.--Salu2 哦,好的。我会去做的。我使用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 thefinal project.Thank you,Gernot (really in despair)If anyone is interested, I want to have a (fast) class set that allowscasting from numbers (variables + constants) to strings and viceversa, also alowing this:cs("A") + some_string + 5 + cs("!")become:"A five 5 !" 这篇关于运营商存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-27 15:43
查看更多