473,473 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Return a container from a class method

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?

Jul 27 '06 #1
10 1545
pa****@gmail.com wrote:
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?
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

Jul 27 '06 #2
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 wrote:
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?

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
Jul 27 '06 #3

mlimber skrev:
pa****@gmail.com wrote:
[snip]

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?

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
}
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.
Cheers! --M
Recheers
Peter

Jul 27 '06 #4
I'm working under linux enviroment with kdevelop3 that use g++.
There is any solution at my question?
Thanks

peter koch wrote:
mlimber skrev:
pa****@gmail.com wrote:
[snip]
>
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?
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
}

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.
Cheers! --M

Recheers
Peter
Jul 27 '06 #5
"padul" <pa****@gmail.comwrote...
I'm working under linux enviroment with kdevelop3 that use g++.
There is any solution at my question?
Thanks

peter koch wrote:
>mlimber skrev:
pa****@gmail.com wrote:
[snip]
>
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?
Why ask if it's possible? Why not just try it? The only negative
side I see is that you keep pointers. Make sure you don't try storing
pointers to local objects in your 'list'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #6
padul wrote:
mlimber wrote:
void ball::split( GLint ntimes, list<ball*>& new_balls )
{
list.clear();
// ... same stuff as before, except for the return
}

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.
First, put your reply to other posts inline or below the post you are
responding to. I've fixed your reply above.

Second, change list.clear() to new_balls.clear(). Mea culpa.

Cheers! --M

Jul 27 '06 #7
peter koch wrote:
mlimber skrev:
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
}

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?
It could be premature optimization, sure. But let's also not
prematurely pessimize.
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.
First, the OP didn't specify the compiler in question. Some don't do
the RVO at all. Second, RVO can only be applied on initialization. For
instance...

vector<intFoo()
{
vector<intv;
v.push_back( 1 );
v.push_back( 2 );
return v;
}

void Bar()
{
vector<intv = Foo(); // RVO applied!
// ... use v somehow ...
v = Foo(); // RVO not applied!
}

For more info, see this section from _Efficient C++ Programming_ by
Lippman:

http://www.awprofessional.com/articl...p?p=25033&rl=1

Cheers! --M

Jul 27 '06 #8

mlimber skrev:
peter koch wrote:
mlimber skrev:
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
}
>
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?

It could be premature optimization, sure. But let's also not
prematurely pessimize.
It is NEVER premature pessimization if the result requires you to write
obfuscated code such as
container <tc;
cfunc(c);
instead of
container <tc = cfunc();
>
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.

First, the OP didn't specify the compiler in question. Some don't do
the RVO at all. Second, RVO can only be applied on initialization. For
instance...
Just out of curiosity. What compiler released in this millenium does
not do RVO or NRVO?
>
vector<intFoo()
{
vector<intv;
v.push_back( 1 );
v.push_back( 2 );
return v;
}

void Bar()
{
vector<intv = Foo(); // RVO applied!
// ... use v somehow ...
v = Foo(); // RVO not applied!
}
Right - but the first solution is still preferable. First, you keep the
natural look of the code (which is most important - code is written to
be read!). Second, you can more easily get the strong exception
guarantee here and lastly this code will be as efficient as your
"return value in a parameter" as soon as the new r-value extensions get
implemented ;-)

/Peter
>
For more info, see this section from _Efficient C++ Programming_ by
Lippman:

http://www.awprofessional.com/articl...p?p=25033&rl=1
An excellent book no longer in my possession ;.-)
Cheers! --M
/Peter

Jul 27 '06 #9
peter koch wrote:
mlimber skrev:
peter koch wrote:
mlimber skrev:
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
}

>
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?
It could be premature optimization, sure. But let's also not
prematurely pessimize.

It is NEVER premature pessimization if the result requires you to write
obfuscated code such as
container <tc;
cfunc(c);
instead of
container <tc = cfunc();
First, the OP didn't specify the compiler in question. Some don't do
the RVO at all. Second, RVO can only be applied on initialization. For
instance...

Just out of curiosity. What compiler released in this millenium does
not do RVO or NRVO?
Well, like I said, the OP didn't specify, and unfortunately, not
everyone has the option of use modern compilers. (Half of my current
project uses VC6, for instance, while the other half uses a quite
compliant EDG-based compiler, and some code has to work on both).

vector<intFoo()
{
vector<intv;
v.push_back( 1 );
v.push_back( 2 );
return v;
}

void Bar()
{
vector<intv = Foo(); // RVO applied!
// ... use v somehow ...
v = Foo(); // RVO not applied!
}

Right - but the first solution is still preferable. First, you keep the
natural look of the code (which is most important - code is written to
be read!). Second, you can more easily get the strong exception
guarantee here and lastly this code will be as efficient as your
"return value in a parameter" as soon as the new r-value extensions get
implemented ;-)
Certainly this is the preferable syntax, but it, too, has a drawback
besides the question of compiler support: it requires the programmer to
remember to make use of RVO and not perform an implicit copy. I see it
as a trade-off, which can be decided one way or the other in specific
instances by the needs of the project, but if a conservative approach
should be taken, then the explicit parameter approach should be used.

Cheers! --M

Jul 28 '06 #10
In article <11**********************@b28g2000cwb.googlegroups .com>,
pa****@gmail.com says...

[ ... ]
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?
This is entirely possible, but depending on your compiler, _may_ be
quite expensive. My own advice would be: instead of dealing with a
container as a whole, have the callee pass an output iterator, and
put the data wherever it points to:

template <class OutIt>
ball::split(GLint ntimes, OutIt &output) {
for (int i=0; i<ntimes; i++)
*output++ = whatever;
}

Just for one example, this allows split to be relatively independent
of the type of container where its output gets put. If (for example)
you find that the overhead of a list is excessive and decide to use a
deque instead, you can do that without changing its code at all.
Likewise, during debugging you might decide to feed its output
directly to an ostream_iterator -- and (again) it won't mind in the
least. In fact, you can perfectly reasonably have some debugging code
that feeds the output directly to a stream, while other code feeds it
to a vector, and still other code feeds it to a list...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 28 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nuno Barros | last post by:
Hello, I am writting a c++ code to crete a kind of a table in memory. Each table has an container of columns, which can be of any type. To do this i created a virtual class Column which has...
17
by: tuvok | last post by:
How can objects of different types be stored in a collection? For example: struct S1 { /*...*/ }; struct S2 { /*...*/ }; struct Sn { /*...*/ }; template<typename T> class X { public:
6
by: Joseph Turian | last post by:
Without modifying the STL code directly, what's the *simplest* way to extend an STL container by adding a method to it? e.g. I want to add method foo() to list<T>. Can I do this without...
16
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
5
by: Tom | last post by:
If I have a container class that has a map member which stores pointers to objects that have been created via the new operator and I have a method that returns a entry in the map, would it be best...
1
by: toton | last post by:
Hi, I need to store some suicidal class to a container like deque. ( I am not using boost container library for pointers at this moment, using stl containers). The container stores some session...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.