Declaring vector of pointer as private class member

Pages: 12
@activecat -
When passing class instance as argument to a function, the whole class instance will be copied before being passed around

That's only true if you're passing by value. If you're passing by reference, then you are in fact passing only a pointer, so there is no difference in efficiency.

In the few cases where you can't pass by reference and must pass by value such as pushing onto a vector, the compiler generates a very efficient move of the object onto the stack. When referencing an object in an STL container, an iterator is a pointer, so there is no difference in efficiency there.

In my experience when using STL containers, the benefits of adding a object to a container by value far outweigh any percieved efficiency of pushing only a pointer.
There are too many opportunities for the pointer and what it points to get out of sync. If you're going to be using pointers with an STL container, you need to be using a unique_ptr.


Passing by reference can generally be more efficient than passing a pointer because it is easier for the compiler to inline the function with the reference than with the pointer. This is what makes std::function callbacks more efficient than function pointer callbacks in many benchmarks - the compiler can more easily inline everything.
closed account (o1vk4iN6)
L B wrote:
Unless it is a very serious issue, in many cases stack unwinding will properly call destructors (which is why a smart pointer should be used).


Still thinking of situations that are uncontrollable by the program, ei killing a process or even Ctrl+C.
@xerzi those are "very serious" issues because the program cannot and should not be held responsible for them. And, at least in Windows, there is SetConsoleCtrlHandler:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686016(v=vs.85).aspx
But still, your program should not be held responsible for it unless you know it would be a major problem not to deal with it.
Last edited on
In fact all sample codes shown here are either passing by pointer, or passing by value. There is no passing by reference at all.

Nevertheless typically a C++ compiler implement a reference as a hidden pointer. So most behaviors of passing by reference apply to the passing by pointers. But both of them are not exactly the same.
Last edited on
Who are you yelling at? There is a profound difference between pointers and references due to the way compilers treat and optimize them.
Sorry this is not to yell at anyone, but to highlight the phrase by grouping the words together: "passing by pointer" and "passing by value".
Maybe I should use italic rather than bold fonts..

I appreciate your advice so far, it helps me a lot.
Thanks.
Last edited on
I guess what I meant was, was your post a question or a statement? It's true that no one here gave an example with explicit pass-by-reference, but why state that?
From sites like http://www.embedded.com/electronics-blogs/programming-pointers/4023307/References-vs-Pointers, it seems that there are significant differences between passing by reference and passing by pointer.

We were looking at examples of passing by pointer but suddenly someone talked about passing by reference. That's why I try to highlight that, because he might be talking about something else.
Last edited on
Dear friends,
Thanks for above comments.

But now I encounter another case which pointer seems unavoidable:
http://www.cplusplus.com/forum/general/95916/
Last edited on
Topic archived. No new replies allowed.
Pages: 12