本文介绍了从类方法返回一个容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好, 我有一个问题:我有一个名为ball的班级使用名为 " split"的方法。 这是ball.h中的声明: class ball:public procObject { public: ball(); ball(GLfloat size,GLfloat x_pos,GLfloat y_pos,GLfloat z_pos); ~ball(); GLvoid draw(GLint模式); //实施 ball * clone(); list< ball * split(GLint ntimes); }; 这是ball.cpp中的实现 list< ball * ball :: split(GLint ntimes) { list< ball * new_balls; ..... .... 返回new_balls; } 是否有可能,如果是,我该怎么做?Hello at all,I have a problem: I have a class named "ball" with a method named"split".This is the declaration in ball.h:class ball : public procObject{public:ball();ball(GLfloat size, GLfloat x_pos, GLfloat y_pos, GLfloat z_pos);~ball();GLvoid draw(GLint mode);//implementedball* clone();list<ball*split(GLint ntimes);};This is th implementation in ball.cpplist<ball*ball::split(GLint ntimes){list<ball*new_balls;.........return new_balls;}Is it possible, and if it is how can I do that?推荐答案 如果你很幸运,编译器可能会优化列表的副本, 但一般来说它不能这样做。更好的方法是将列表作为参考传递给 : void ball :: split(GLint ntimes,list< ball *>& ; new_balls) { list.clear(); // ...与以前相同的东西,除了返回 } 干杯! --MIf you''re lucky, the compiler might optimize the copy of the list away,but generally speaking it can''t do so. A better way would be to passthe list in as a reference:void ball::split( GLint ntimes, list<ball*>& new_balls ){list.clear();// ... same stuff as before, except for the return}Cheers! --M 如果你很幸运,编译器可能会优化列表的副本, 但一般来说它不可能这样做。更好的方法是将列表作为参考传递给 : void ball :: split(GLint ntimes,list< ball *>& ; new_balls) { list.clear(); // ...与以前相同的东西,除了返回 } 干杯! --MIf you''re lucky, the compiler might optimize the copy of the list away,but generally speaking it can''t do so. A better way would be to passthe list in as a reference: void ball::split( GLint ntimes, list<ball*>& new_balls ) { list.clear(); // ... same stuff as before, except for the return }Cheers! --M [snip][snip] 如果你很幸运,编译器可能会优化列表的副本, 但一般来说它不可能这样做。更好的方法是将列表作为参考传递给 : void ball :: split(GLint ntimes,list< ball *>& ; new_balls) { list.clear(); // ...与以前相同的东西,除了返回 }If you''re lucky, the compiler might optimize the copy of the list away,but generally speaking it can''t do so. A better way would be to passthe list in as a reference: void ball::split( GLint ntimes, list<ball*>& new_balls ) { list.clear(); // ... same stuff as before, except for the return } 我有两个问题:第一个为什么你选择强制使用奇怪的语法 因为某些优化不会发生。是否有任何迹象 有问题的程序是在昂贵的路径上? 我的第二个问题是你认为优化器不能做RVO的原因。 /> 我使用的编译器都在没有任何问题的情况下执行RVO 优化已打开。在这种情况下,你的代码不仅更多,而且更加自然 - 它也更慢。I have two questions: first why you choose to force a weird syntaxbecause some optimization not would take place. Is there any indicationthat the procedure in question is on an expensive path?My second question is why you believe the optimizer can''t do the RVO.The compilers I use all perform RVO without any problems providedoptimizations are turned on. In that case your code is not only moreverbose and less natural - it is also slower. Recheers PeterRecheersPeter 这篇关于从类方法返回一个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 03:13
查看更多