I am a new computing science student and not very experienced in c++. I've been googling for hours, and did not come across a solution(to be honest, i couldn't even find a good statement to search). Basically what i'm trying to learn here is how to implement a copy operator for a case like:
#include <string>
#include <vector>
class foo
{
foo* parentObject;
std::vector<foo*> childObjects;
char a;
int *b;
public:
foo(char a, int* b) : a(a), b(b) {};
foo();
foo& operator =(const foo& other)
{
a = other.a;
b = new int(*other.b);
//parentobject???
//pretty sure i can figure childObjects if i can manageparentObject,
//but just in case there are some special stuff wanted to include.
return *this;
}
friend void getElements(std::string fpath);
};
To go deeper, i want to read objects of the same type that are in a tree-like hierarchy(sort of like XML) from a formatted text file and have only one instance of the object i read in the memory(lets assume thats what getElements does), and the rest are pointers to those objects(we can think of this as how shortcuts work). That is because, i thought that would be the most efficient method since there can be a lot of objects, please correct me if i'm wrong.
But i also want to keep track of the structure so i decided to go with this parent/child design(again could be a bad choice, feel free to enlighten me). Because of the reading method i used(from parent to children), even though i can find a way to initialize parentObject, i can't simply initialize childObjects. Therefore i can't use a 'new constructor()' assignment(or maybe i can but i just don't know that i can).
If the reason behind my need of (shallow)copy operator is unclear, since i didn't initialize parentObject or childObjects, i need to assign them later on while reading the text document(it really feels like i shouldn't need one but i think i do.).
My actual code is really hard to read; since this is a project just to get better at c++, i didn't give importance to coding style) and it is longer so i created a simpler version. I really appreciate any comment so feel free to write one.
Edit: I couldn't even copy paste properly(initialization of b). Edit: Clearence.