Structure member finding

Hi,

I am beginner to C++.
I would like to know if I can find all members of a structure when I have no information about its members.
The reason for asking this question is that I am working on a big code that it would be so time consuming to search in the libraries to find the structure of the data. Thanks in advance.

1
2
3
4
5
6
7
  struct structure{
  ... // something here that I have no access to!
  };
  
  structure p;
  cout<<something.p<<endl // I need to find all members of the structure by cout or anything else
C/C++ does not support refelection – unlike, for example, Java or C#.

This means that, in C/C++, you can not "detect" the members of an unknown struct (or class) at runtime, you have to know it at compile-time – usually by #include'ing the header file where the struct is defined. In fact, you can not access the content of an undefined struct at all – it is only possible to pass around pointers.

(You won't be able to de-reference a pointer to an undefined struct though)

So, you probably want to use an std::map or std::unordered_map instead then! Simply put, a map stores key-value pairs, you can enumerate the existing keys at runtime, and even dynamically insert more keys.

___________________

You can get, very limited, runtime type information via the typeid operator:
https://en.cppreference.com/w/cpp/language/typeid
Last edited on
Well if your compiler supports intellisense (like VS), then if you move the mouse over the variable name of the structure then you see a list of member names.
"IDE", not "compiler".
I am working on a big code that it would be so time consuming to search in the libraries to find the structure of the data.

Code is arranged in "headers" and "sources" to keep the type definitions neatly separate from implementations. On precompiled libraries you get the binary that contains implementations and the headers so that you can use the defined types; all the code you can easily read is headers -- definitions.

A well written library provides also (searchable) documentation that describes the interface; types and functions.

If you don't have access to definition of struct structure, then you cannot use it in your code; cannot compile.

If I had a directory tree with untold lines of code in many many files, I could do:
grep -r structure .

Recursively check every file in this directory and all subdirectories and print all lines that contain word "structure" (and tell the file too).
GNU grep is regular expression file search tool. Windows has something similar, but I have grep installed on Windows too.
basically what they said, with some more..

first, visual studio is your friend. Here is why:
not only does it list the members of an object when you type a . (eg p. in your example would list all P's things), it also has 2 more great tools. First is the class view, which is a crude hierarchy of inheritance mostly. But if you get there for a type, you can pop it open in the class wizard, which lists the types very cleanly with tabs for the windows junk if its a GUI object and tabs for normal classes that list its variables, methods, and virtual methods.

second, grep is available for windows. I personally install the cygwin suite to get this and other similar unix console commands embedded into my windows cmd console for quick searches. My project for work has about 400MB of source files, just the text-code that is, and grep can find anything in it in just a few seconds if you keep the regx simple.

if you want more than this, a paid-for tool called 'understand c++' gives every possible detail on things like ... what is in a class, what is derived from it, what it is derived from, where it is used, where each method is called, and more -- it can be configured to expound upon those things with the comments in the code if you have any consistent commenting style (you can define the style). I don't know if there is anything like it for free, or what similar tools are out there. Normally the info from visual is more than enough.

If you don't want to use visual studio, you need to find something that, at a minimum, provides the field breakout on typing the period and can list your classes & structs with members and methods. I would think any real IDE would have that these days, but make sure yours does before committing to it.
Topic archived. No new replies allowed.