Tag Info

New answers tagged

0

The problem in my mind is that whether you use a reference or a pointer, your class has a view into data that is created separately, may be destroyed separately, and the class has no way of knowing that. Foo f; if (something) { Bar x; // stuff with x f.Register(x); } f.DoSomething(); // screwed! It's not a good sign for such innocent code ...


2

Preparing my flame-retardant suit, as I feel some bias must be present in any answer to this question. First, I would like to note that I work within the embedded world, so RAII and stack allocation are generally the way to go. In almost any circumstance, I see passing something in by reference as a good idea because of the obvious reason: it can't be NULL. ...


0

In a typical situation, you only pass a pointer if you want to accept nullptr or the typical usage of the type is not as a value. This is, for example, some (but not all!) interfaces. In most cases you always take a (potentially const) reference.


-2

Our coding guide -- written partially by me :-) -- says to always pass by pointer in this case where you want to hold on to a pointer. With pointers, you can mostly correctly use const and non const versions, whatever is more appropriate (i.e., you may only want to hold on to a const Bar*, in which case you then can also only pass a const Bar*) Using a ...



Top 50 recent answers are included