I was trying to return an object UnitCubeInstance
, which is an instance of a class MeshObject
. The MeshObject
has a reference to the MeshAsset
class.
My current way of doing this is of no luck, and when the object is returned, the MeshAsset
is no longer accessible.
My question is, is the design shown below viable in any way? Or do I have to make MeshObject
to hold actual instances of MeshAsset
, which seems easy to do but not efficient on the memory.
The problematic code:
static MeshObject createUnitCubeInstance(Program &program) {
auto unitCubeAsset = MeshAsset::UnitCube();
auto unitCubeInstance = MeshObject(unitCubeAsset);
return unitCubeInstance;
}
And a minimal version of the two classes:
class MeshAsset {
// returns a general instance that is designed to be shared,
// kind of resemble a singleton
static MeshAsset UnitCube();
}
class MeshObject {
MeshAsset& asset;
MeshObject(MeshAsset& asset);
}
If I missed something that is crucial for my example, please feel free to comment.