从类方法返回一个容器 [英] Return a container from a class method

查看:64
本文介绍了从类方法返回一个容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个问题:我有一个名为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); //implemented
ball* clone();

list<ball*split(GLint ntimes);
};

This is th implementation in ball.cpp

list<ball*ball::split(GLint ntimes)
{
list<ball*new_balls;
.....
....

return new_balls;

}

Is it possible, and if it is how can I do that?

推荐答案

pa **** @ gmail.com 写道:

你好,

我有一个问题:我有一个名为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); //implemented
ball* clone();

list<ball*split(GLint ntimes);
};

This is th implementation in ball.cpp

list<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();

// ...与以前相同的东西,除了返回

}


干杯! --M

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 pass
the 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


编译器返回,我这些错误:


ball.h:23:错误: ''list''尚未宣布

ball.h:23:错误:预期'',''或'''''在''<''之前
无论如何都要解决这个问题,我知道如何传递一个容器作为

参数从一个类的方法返回一个容器。

谢谢。

Padul


mlimber写道:
The compiler return ,me these errors:

ball.h:23: error: ''list'' has not been declared
ball.h:23: error: expected '','' or ''...'' before ''<'' to
is there anyway to resolve this problem, I would know how to pass as
parameter a container o return a container from a method of a class.
Thanks.
Padul

mlimber wrote:
pa **** @ gmail.com 写道:

你好,

我有一个问题:我有一个名为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); //implemented
ball* clone();

list<ball*split(GLint ntimes);
};

This is th implementation in ball.cpp

list<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();

// ...与以前相同的东西,除了返回

}


干杯! --M


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 pass
the 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




mlimber skrev:

mlimber skrev:
pa **** @ gmail.com 写道:



[snip]

[snip]



这是ball.cpp中的实现


list< ball * ball :: split(GLint ntimes)

{

list< ball * new_balls;

....

...


返回new_balls;


}


是否有可能,如果有的话我该怎么办?

This is th implementation in ball.cpp

list<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();

// ...与以前相同的东西,除了返回

}


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 pass
the 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 syntax
because some optimization not would take place. Is there any indication
that 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 provided
optimizations are turned on. In that case your code is not only more
verbose and less natural - it is also slower.


干杯! --M
Cheers! --M



Recheers

Peter

Recheers
Peter


这篇关于从类方法返回一个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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