Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object containing lists of itself recursively

Tags:

c++

object

For practice, I am trying to make a recursive directory parser.

For the sake of parsimony, I want to contain the result recursively too, e.g.:

1 class CDirectory
2 {
3     private:
4        std::string name;
5        std::vector<CDirectory> subDirectories
6    public:
7        //Various things, constructors etc. go here
8 }

However, I see here that line 5 is not supported behavior - "The C++ Standard (2003) clearly says that instantiating a standard container with an incomplete type, invokes undefined-behavior."

What, then, do I do? Is there no way to make an object contain a list of similar objects? If nothing else, I know that it is by no means illegal to make a vector of vectors, so that's an object that contains itself.

like image 734
shieldfoss Avatar asked Dec 16 '14 13:12

shieldfoss


1 Answers

Boost has containers that support incomplete types. You can use one of these.

#include <boost/container/vector.hpp>

class CDirectory
{
   private:
       std::string name;
       boost::container::vector<CDirectory> subDirectories
   public:
       //Various things, constructors etc. go here
};
like image 187
juanchopanza Avatar answered Nov 01 '22 14:11

juanchopanza