0

This is my program

struct All_Modules
{
    char* Name;
};

All_Modules First_Array[] = { { "hi\n" } };
All_Modules Next_Array[]  = { { "hi1\n" } };
All_Modules Other_Array[] = { { "hi2\n" } };

int main()
{
}

Is there way to get list of All_Modules all array name like (First_Array,...) and show print them like this?

My array is : First_Array | and The of This array is : hi
My array is : Next_Array | and The of This array is : hi1
My array is : Other_Array | and The of This array is : hi2

I know we can set each array to print this but I want first system get the list of all array in All_Modules and detect the names and automatic print the Name value... Is it possible?

1
  • You make an awful lot of questions just in your title. Which of the several steps do you have a problem with? Please adjust your question so that this becomes obvious. Commented Aug 24, 2015 at 6:29

2 Answers 2

1

Yeah, it is possible, but you have to implement it by yourself using static members.

In the constructor of All_Modules append newly created instance to a global (static) list.

struct All_Modules{
  char* Name;

  All_Modules(){
    gModulesList.push_back(this);
  };

  static void print_all(){
    for(... gModulesList ...){
      // print gModulesList[i]->Name;
    }
  };

private:
  static std::list<All_Modules*> gModulesList;  
};
Sign up to request clarification or add additional context in comments.

1 Comment

when i want to add an array from All_Modules, i see this error 'initializing': cannot convert from 'initializer list' to 'All_Modules' Project2
0

In order to do that, you would have to enumerate all global variables for those of a certain type, but C++ doesn't offer a way for that (it's called introspection, BTW). There might be some compiler-specific ways to get at that info though, but they won't be portable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.