2

I need to instantiate an object from a class, let's say Cat, and know the names of its included files.

The main program should look something like this:

    #include <iostream>
    #include <vector>
    #include "Cat.h"

    using namespace std;

    vector<string> getIncludes(Cat cat){
      ...
    }

    int main(int argc, char *argv[])
    {
      Cat cat;
      std::vector<string>list = getIncludes(cat);
    }

And the Cat class header file would be like this:

    #include "utils.h"
    #include "animals.h"

    class Cat{
    public:
      Cat();
      ~Cat();
      void meow();
    private:
      int age;
    }

After calling std::vector<string> list = getIncludes(cat), list should contain "utils.h" and "animals.h".

If there is another way to get those names in the main function I am open to suggestions. Thank you in advance.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Camilo
  • 83
  • 1
  • 8

1 Answers1

5

No, that's not possible, the information won't be present anymore at runtime.

The preprocessor simply replaces the contents of utils.h and animals.h at compile time at the place where the #include statements appear.


If you need such a list to detect dependencies for recompiling your code, most of the compilers can generate a list of the included headers in a translation unit.

Here's some further information how to do so:

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thank you @πάντα ῥεῖ I will find out how to get the info from the compiler, then. – Camilo Jun 26 '17 at 15:29
  • 2
    @Camilo You should probably explain your actual use case that requires you doing that in your question. It really sounds like a XY-problem as stated in the 1st comment. – πάντα ῥεῖ Jun 26 '17 at 15:46
  • It is an XY-problem, I suppose. My use case is the following: I have a class with some private attributes that implement some interfaces (e.g.: a Cat that implements CatInterface) and, since I cannot access those attributes nor I know apriori what interfaces the classes will be implementing, I thought I could use Cat's header file to know what interfaces it is including. – Camilo Jun 27 '17 at 08:38
  • @Camilo And how should listing the include files help about accessing the private attributes? I believe you should look up for dynamic polymorphism, and how to design the `Animal` interface instead. – πάντα ῥεῖ Jun 27 '17 at 08:41
  • To summarise: from a Cat myCat object I need to extract what interfaces its attributes are implementing. @πάντα ῥεῖ I don't need to access the private attributes, only know what interfaces they are using – Camilo Jun 27 '17 at 08:44
  • @Camilo If your `Cat` class implements any interfaces like `Animal` you can check that either with [`std::is_base_of`](http://en.cppreference.com/w/cpp/types/is_base_of) at compile time, or applying a `dynamic_cast` at runtime. – πάντα ῥεῖ Jun 27 '17 at 08:47
  • @Camilo Please formulate another question and specify exactly what you need to do. Also it's a bit unclear why you need to know what particular interfaces are used for attributes (sounds like another XY-problem right now). – πάντα ῥεῖ Jun 27 '17 at 08:49
  • I will rethink my problem and come back whenever I have a more specific question. Thanks a lot for your help! @πάντα ῥεῖ – Camilo Jun 27 '17 at 08:53
  • @Camilo As mentioned, you can provide a complete example code of what you have already, and what you expect to do it in another question. I believe your problem will be much easier to solve than you're thinking about it ATM. – πάντα ῥεῖ Jun 27 '17 at 08:55