Re: returning a vector from a funciton

From: Affan Syed (asyed_at_usc.edu)
Date: 11/27/04


Date: Sat, 27 Nov 2004 12:33:51 -0800

That is exactly the idea that i am trying to do now.. It clicked me as soon
as I had posted the note., and to top it it works perfectly fine.
Thanks
Affan
"Michael" <slick_mick_00@hotmail.com> wrote in message
news:coa5nv$n4$1@titan.btinternet.com...
> will it be quicker to do:
>
> bool GetVector( vector<myClass>& v)
> {
> assert(v.size() == 0);
> //Populate v;
>
> return true;
> }
>
> int main()
> {
> vector<myClass> myVec;
> if( GetVector(myVec) )
> {
> }
> }
>
> ??
> To reduce copying of data items?
>
> Regard Mike
>
>
> "Ali Cehreli" <acehreli@yahoo.com> wrote in message
> news:pan.2004.11.27.11.27.54.421113@yahoo.com...
>> > I have used vectors (STL) for some time but have
>> > never had the need to actually return one from a function. what i mean
> is
>> > that does something like the code below work?
>>
>> It works; I do that all the time.
>>
>> > vector<myClass> RetVector(){
>> > vector<myClass> retVector;
>> > .
>> > /// add a few myClass objects to the vector
>> > .
>> > return retVector;
>> > }
>> >
>> > int main (){
>> > vector<myClass> getVector;
>> >
>> > getVector = RetVector();
>>
>> The code above has default construction and assignment. You might
>> prefer
>>
>> vector<myClass> getVector = RetVector();
>>
>> which will involve just the construction of the getVector object under
>> many compilers. You should try it in your environment...
>>
>> >
>> > ///use the vector to do some computation
>> > return 1;
>>
>> Returning 1 normally indicates error. EXIT_SUCCESS is a better choice
>> for successful return from main.
>>
>> > }
>> >
>> >
>> > What i mean is that does the copy constructor do the right thing and I
> now
>> > have faithfull copy of the orignal vector that i can now use to index
>> > through and play to my hearts content?
>>
>> Yes. Again though, the way you wrote it, the assignment operator is
>> used, not the copy constructor.
>>
>> Now it all depends on the implementation of myClass (e.g. the
>> assignment operator, the copy constructor, etc.) As long as it's
>> implemented correctly, you are safe.
>>
>> Ali
>>
>
>



Relevant Pages

  • Re: support for F90/F95 in SCons
    ... Check out the FORTRAN* construction variables. ... variables that you can set in an environment. ... overriding the defaults built in to SCons. ... where to find compilers that may be in your path. ...
    (comp.lang.fortran)
  • Re: Copy Constructor for an Array Element ???
    ... > I'd recommend using an assignment operator or a member function assign ... rather than the single integrated step of copy construction. ... make sure to call the objects destructor first ...
    (comp.lang.cpp)
  • Re: Template casting operator
    ... > generated assignment operator and a construction of a temporary ... > No special assignment operator is needed here. ... Jonathan ...
    (comp.lang.cpp)
  • NARMADA DAMS - Construction, at any cost
    ... A fresh controversy is brewing around the Sardar Sarovar Narmada ... ahead for further construction at the dam. ... recommendation of its own committee set up to assess the compliance of ... The committee's report was considered at a meeting of the Environment ...
    (talk.politics.misc)
  • NARMADA DAMS - Construction, at any cost
    ... A fresh controversy is brewing around the Sardar Sarovar Narmada ... ahead for further construction at the dam. ... recommendation of its own committee set up to assess the compliance of ... The committee's report was considered at a meeting of the Environment ...
    (sci.military.naval)