In addiction to other solution, consider also a better design of your classes, or your code will became a mess.
a) You use sometimes
string
(did you mean std::string ??) sometime character arrays, sometime char*. Be consistent!
b) Directories should form a hierarchy. How can you do it with just one pointer?
c) You already mix-up classes and arrays improperly:
string fname[25]
is a array of 25
string
-s.
fname=NULL
is meaningless (and can lead to disasters)
d) Where does the
25
magic number comes from ?
e) Are
file
and
directory
completely unrelated beasts or should they have a common ancestor (a sort of
filesytementity
) managing names and conjunctions?
consider this:
#include <string>
#include <list>
#include <iostream>
class file;
class directory;
class filesystementity
{
friend class directory;
private:
std::string name; directory* owner; public:
virtual ~filesystementity() {}
virtual void set_name(const std::string s); virtual std::string get_name() const; virtual std::string get_pathname() const; virtual directory* get_owner() const; };
class directory: public filesystementity
{
private:
typedef std::list<filesystementity*> entitylist;
entitylist entries; public:
~directory(); directory(const std::string& path); typedef entitylist::iterator iterator;
iterator begin() { return entries.begin(); }
iterator end() { return entries.end(); }
static directory* is_dir(iterator i) { return dynamic_cast<directory*>(*i); }
static file* is_file(iterator i) { return dynamic_cast<file*>(*i); }
};
class file: public filesystementity
{
public:
};
Dynamic memory is used only for polymorphic objects, not for string related stuffs.