> immutable class
So it is pretty much what I thought: construct once and never change internal state after that.
> Of course, one could argue about its relevance today when memory is cheap.
Nah, having pointers to common data rather than multiple copies is viable. Cache is limited and copying objects in memory takes time.
> Maybe I'm misinterpreting but as far as I can tell, if I supply the "stuff" through the constructor of the class then the parameters are copied.
If you use non-pointers, then they get copied. If you use pointers, then they don't. References confuse me to this day, so just read up on them, I guess.
> If I now create bar with as a pointer to A and inject it to Foo, then after deleting the pointer and freeing the memory either [...]
It took me a while to wrap my head around what you're trying to accomplish here. I still don't understand why you insist on using references instead of pointers like God commanded it. Here's what I'd do:
class Foo
{
private:
shared_ptr<A> instanceName; // TODO: Put const in a few random places and see if it still compiles.
// stuff
public:
Foo(const shared_ptr<A> &aptr) : instanceName(aptr) { } // Definietely OK to pass aptr by value and probably OK to pass by reference.
// stuff
}
Then
std::shared_ptr<int> ap (new A(...));
Then just use ap as need, for example:
Foo f1(ap);
Foo f2(ap);
For this to work object A must be on the heap of course (well, unless you create it on the stack in a place that will assure it exists for as long as needed), but it is still one operator new and no copying after that and lifetime of object pointed to by ap is taken care of.
The way you're trying to accomplish it, namely "thingBeingPointedTo(bar)" in initialiser list is a call to copy constructor and that's most likely going to copy memory. On top of that class Foo would also need to waste enough memory to fit an A object inside, which you don't even want to use. If you create an object, pass it by reference, take the address of it and finally delete the thing, you'll get a dangling pointer. Yep, I think you're a java programmer.
> if you ever so much as looked at a software engineering course, you'll know that ease of use is generally the very last thing that those companies worry about when choosing their tools.
What? Software engineering courses teach some maths, basic programming, algorithms, space-time complexity and things like that. They aren't concerned with "what the company will want".
On the other hand, when I went to my third job, my colleagues are complaining about their clients wanting Sharepoint and other frameworks and how horrible those tools are for anything beyond prototyping.