OK, so I'll follow #1 and maybe after finishing the whole project I will try to change it with using #2. Is it the proper way? (I've got a problem with destructing these children)
Greetings!Code:#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Directory
{
private:
Directory(const Directory& dir);
void operator=(const Directory& dir);
public:
string name;
vector<Directory*> children;
Directory(string name_) : name(name_) { };
~Directory() {delete children;} ; //I guess it's not a proper way of destructing
};
//what should I write inside constructor and operator?
Directory::Directory(const Directory& dir)
{}
void Directory::operator=(const Directory& dir);
{}
int nesting = 0;
void print(Directory &dir) { //"Your print function needs to take a reference, not a pointer"...
for (int i = 0; i < nesting; ++i) cout << ' ';
cout << dir.name << endl; //...so I had to change this...
++nesting;
for_each(dir.children.begin(), dir.children.end(), print); //...and this.
--nesting;
}
int main(void) {
/*
- root
- sub1
- sub1.1
- sub2
- sub3
- sub3.1
- sub3.1.1
*/
Directory root("root");// = new Directory("root"); //"you'd want to leave root as an object"
Directory *sub1 = new Directory("sub1");
Directory *sub11 = new Directory("sub1.1");
Directory *sub2 = new Directory("sub2");
Directory *sub3 = new Directory("sub3");
Directory *sub31 = new Directory("sub3.1");
Directory *sub311 = new Directory("sub3.1.1");
root->children.push_back(sub1);
sub1->children.push_back(sub11);
root->children.push_back(sub2);
root->children.push_back(sub3);
sub3->children.push_back(sub31);
sub31->children.push_back(sub311);
//print(root);
system("pause");
return 0;
}