c/c++ docstrings

Is there a way you can pull a docstring in c++? Kind of like python docstrings.
Even defining my own classes i forget what they do and have to look back at its documentation to revive my memory. It would make it simpler if i didnt have to go find the definition of that method, and just print its docstring for quick refernece.

Here would be an example of what i mean. Of course i could only think of making a whole new function named the same with the added _help() to accomodate the docstring style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

class Utility{
    public:
        std::string reverse(std::string s){
            return std::string(s.rbegin(), s.rend()) + "\n";
        }
        std::string reverse_help(){
            std::string s = "std::string reverse(std::string s) ";
            s += "return s reversed string with newline added";
            return s;
        }
};

int main(){
    Utility util;
	std::string s = "barack obama";
    std::cout << util.reverse(s) << std::endl;
    std::cout << util.reverse_help() << std::endl;
}


but it would make it a lot easier if there was a method to print the comment below the prototype of a function. Something like:
1
2
3
4
5
6
void reverse(){
    /*
     my docstring
     */
     cout << '\n';
}

where it would print my docstring?


Last edited on
Try using a comment system like doxygen? It can generate html documentation pages from comments in a program.
http://www.stack.nl/~dimitri/doxygen/
hmm not sure but thanks for the link, i will check it out. In the meantime, it appears that you cannot just grab all class variables, so you have to push each one into the vector? I used a vector instead of an array so i can use vector.size() with util.help(), so i dont have to send in the size when i call it, because i wouldnt know the size off hand anyways. The only pain in the ass thing then is it cannot dynamically add its docstrings to its attributes attr. it has to be manually added every time i add a new class member function. But i gue4ss its close as it can be?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <vector>

class Utility{
    public:
        std::string reverse_help = "std::string reverse(std::string s) \n\treturn s reversed";
        std::string print_help = "void print(std::string s)\nvoid print(int i) \n\tprint string or int";
        std::vector<std::string> attr = {
            reverse_help,
            print_help
        };
        std::string help(){
            std::string s;
            for (int i=0; i<attr.size(); i++){
                s += attr[i] + "\n";
            }
            return s;
        }
        
        
        
        
        std::string reverse(std::string s){
            return std::string(s.rbegin(), s.rend());
        }
        void print(std::string s){
            std::cout << s << std::endl;
        }
        void print(int i){
            std::cout << i << std::endl;
        }

};

int main(){
    Utility util;

    std::cout << util.help() << std::endl;
    std::cout << util.reverse_help << std::endl;
}
Last edited on
Topic archived. No new replies allowed.