#Iterating struct

10 messages · Page 1 of 1 (latest)

ashen cove
#

I wonder that is there a possible way to interate every element of a struct?

struct Request {
    enum method { // (R)equest (M)ethod + <method name>
        RMstatus,
        RMnew,
        RMkill,
        RMenable,
        RMdisable,
        RMexit
    } request_method;
    enum type { // (R)equest (T)ype + <type name>
        RTsync,
        RTpush,
        RTpull
    } request_type;
    std::string orig_path;
    std::string dest_path;
    int freq;

    Request(method _method, type _type, std::string o_path, std::string d_path) :
        request_method(_method), request_type(_type), orig_path(o_path), dest_path(d_path) {
    }

    friend std::ostream& operator<<(std::ostream& os, Request const& request) {
        os << "method: " << request.request_method
            << " type: " << request.request_type
            << " orig_path: " << request.orig_path
            << " dest_path: " << request.dest_path;
        return os;
    }
};

how can i access each element by using a loop
any help would be thankful...

edgy pewterBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

glacial jungle
#

C++ itself doesn't support that

#

But you could create your own reflection system to do that, but that's not like.. the easiest thing to do

ashen cove
#

do we have to use a template to do a reflection?

#

or is there a mature solution

glacial jungle
#

There are probably many ways, but maybe it would be the best to just use a third party lib if you really need that.
Boost for example should have something for that

ashen cove
#

I will look for that. Thanks for you help!

edgy pewterBOT
#

@ashen cove Has your question been resolved? If so, run !solved :)