这是main.cpp
Directory* c = dynamic_cast<Directory*>(r->getChild("c"));
cout << c->addChild(true, "ca") << endl;
cout << c->addChild(true, "cn") << endl;
cout << c->addChild(false, "cf1") << endl;
cout << c->list() << endl;
cout << "================= Test 6 =================" << endl;
cout << c->getPath() << endl;
cout << c->getChild("cn")->getPath() << endl;
cout << c->getChild("cf1")->getPath() << endl;
这是我的预期输出:
================= Test 6 =================
\c
\c\cn
\c\cf1
这是我的基类-FSObject
class FSObject
{
private:
string name;
FSObject* parent;
}
这是我继承的类-目录
class Directory : public FSObject
{
public:
Directory(string name, FSObject* parent) : FSObject(name, parent) {};
}
这是我继承的类-SADirectory
class SADirectory : public Directory
{
public:
SADirectory(string name, FSObject* parent);
private:
FSObject* children[DIR_SIZE];
int count;
}
这是我另一个继承的类-File
class File : public FSObject
{
private:
string content; //the string content
}
考虑到main.cpp,addChild函数可用于添加文件和sa目录。 但是,问题是,如何在类FSObject中获得类似于预期输出的结果来实现getPath()? 谢谢您的帮助。
这是我尝试递归gatPath()
string FSObject::getPath(){
if(this->getPath()=="\\"){
return "";
}
string temp = "\\";
temp += this->parent->getPath() + this->name;
return temp;
}