I have some type like this:
#pragma once
#include <stdio.h>
#include <string>
struct Color {
std::string value;
};
struct Shirt {
std::string brand;
Color color;
};
class Outfit {
public:
Outfit(Shirt shirt):
shirt(shirt) {}
private:
Shirt shirt;
};
I previously reviewed this Stackoverflow thread on this topic.
Let me explain my thinking, and I'd like for anyone here to tell me if I'm correct or incorrect or if there's something I didn't consider or misunderstood.
My current understanding is I should pass in the arguments as const reference. If I pass by value, then when I create an instance of Outfit
, for example, Shirt
will be be copied. If I understood correctly, this might be dangerous. The copy and the original Outfit will point to the same Shirt
instance. And this shared ownership model is dangerous because we don't know if Shirt gets released.
Is that understanding correct? Or am I overcomplicating this?