If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

STL and pointer/reference question

Hi all,

Im rather new to C++ (I´m changing from Java to C++), so please bear with me.
I have a question about STL and returning a vector array
from a static method.

How do I do it correctly, so that the function contructs
a vector that can be returned to the caller. I know if I
create a local object from a class and return it`s reference, it will point to null (if I´m rigth) after the
function call. So the object should be created with the new
operator. But how do I do it with template call??
I just get errors when I try to compile.

It should go something like this(please, do correct my syntax, pointers and references are used very wrong, I think):

Originally posted by halmark6Z
Thanks for your replies. Helped a lot, really!

But, could you explain little more futher this:

How it prevents a local copy from going out of scope?

again, many thanks!

Consider the following:

Code:

int foo()
{
int i = 0;
return i;
}
int main()
{
int myval = foo();
}

Note that the foo() function has created a local int, i, and returns it by value. The main() function calls foo() and assigns it to an int.

Compare this simple example using int's with the example using std::vector<> -- the only difference is the type returned. That is the beauty of C++. If your class provides a copy constructor and assignment operator, the class acts just like one of the basic types when returning by value. For vector, the assignment operator copies one vector to another, and the copy ctor creates a vector from an existing vector. So when you do a return of std::vector<>, you are invoking the copy constructor.

So in lay terms, you didn't question whether the local int in the foo()goes out of scope, right? So why question whether the vector goes out of scope? -- it follows the same rules of return by value. Now, if you return a pointer or reference to the local, then scope issues are a concern. My vector code does neither -- it returns by value, just like the int foo() example does.

The only other concern is if your class does not implement a properly behaved copy constructor and assignment operator (i.e. the class uses the default copy and assignment when it shouldn't be using them, or you just coded user-defined versions of these functions incorrectly), then of course, returning by value still "works", but it will introduce a bug.

Java and C# (AFAIK) don't actually copy objects: if you say "a = b" (where a and b have class type), then the object referred to by "a" has its reference count decremented, and the object referred to by "b"has its reference count incremented to reflect that it is now referred to by "a" as well. In C++, the same statement creates two independent copies of the same data - whatever data "a" originally contained is lost (overwritten). No reference counts involved. C++ doesn't do reference counting unless you implement it yourself. This is why copy constructors, copy assignment operators and destructors are so important - C++ makes you do your own resource management. Oh, and I should say that, IMHO although that may seem to be a disadvantage, it actually gives you a lot more flexibility than garbage collection once you're used to it.

Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.-- Sutter and Alexandrescu, C++ Coding Standards

Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman

The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell

One source of confusion is that Java has call by value only. C and Java has call by value only, Fortran has call by reference only, but Pascal and C++ has a mixed calling mechanism.

When someone from Java says she's passing something by reference she actually means she's passing a reference by value.

So in this case what the OP, coming from Java, actually asks is how can I create a collection in a function, and then pass it back to the calling function without copying. The reason is efficiency. If you create say 1 million integers in an STL container in a function you don't want it to be copied back to the calling function. What you want back is a pointer (a reference) to the collection.

One source of confusion is that Java has call by value only. C and Java has call by value only, Fortran has call by reference only, but Pascal and C++ has a mixed calling mechanism.

When someone from Java says she's passing something by reference she actually means she's passing a reference by value.

Actually the only call-by-value behavior in Java is for primitives, everything else is passed by reference (as there are only references in Java).
C has call by value or by reference (using pointers).
C++ has call by value or by reference (using either pointers or references).

Originally posted by CornedBee
Actually the only call-by-value behavior in Java is for primitives, everything else is passed by reference (as there are only references in Java).
C has call by value or by reference (using pointers).
C++ has call by value or by reference (using either pointers or references).

I didn't expect to find this misconception here. The only parameter passing mechanism in Java is by value. Period. Pass by reference was considered error prone and ruled out by the founders of Java. C also has call by value only. Passing a pointer (reference) by value is NOT the same as pass by reference.

If you need the efficiency of not copying the vector, pass one in.

That's one possibility. The other one is to create a new vector on the heap in the function and then return a pointer to it.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.