Topic: Can a class know names of its own data-members?


Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/06/28
Raw View
Hi,

Here are a program and a question.

        Thanks in advance,
        Alex



//#########################################################
//------------------- C++ code : BEGIN -------------------
#include <iostream>
#include <typeinfo>

class BBB {};
class AAA
{
        public :
                int     abcd_;
                char    prq_;
                BBB     xyz_;
};

int main ()
{
AAA a1;
        // A class knows its own name (that is typename!)
        // and typenames of its own data-members.
        cout << typeid(AAA).name () << endl;
        cout << typeid(a1).name () << endl;

        cout << typeid(AAA::abcd_).name () << endl;
        cout << typeid(a1.abcd_).name () << endl;

        cout << typeid(AAA::prq_).name () << endl;
        cout << typeid(a1.prq_).name () << endl;

        cout << typeid(AAA::xyz_).name () << endl;
        cout << typeid(a1.xyz_).name () << endl;

        //=================
        /*
        #######################################
        Can a class know number and names of its own data-members?
        Something like this:

        cout << attribute (AAA).total () << endl;

        cout << attribute (AAA).name (1) << endl;
        cout << attribute (AAA).name (2) << endl;
        cout << attribute (AAA).name (3) << endl;

        cout << attribute (AAA).name (abcd_) << endl;
        cout << attribute (AAA).name (prq_) << endl;
        cout << attribute (AAA).name (xyz_) << endl;

        cout << attribute (AAA).names () << endl;

        I suppose the answer is "No". Then :
                1) Why is a such possibility missing?
                2) Are there any plans to add this feature to C++?


        #######################################
        */

        return 0;
}

//------------------- C++ code : END ----------------------




//#########################################################
//------------------- Running Results : BEGIN -------------
3AAA
3AAA
i
i
c
c
3BBB
3BBB


######### Here is what I would like to get #########
3       // Number of data-members
abcd_   // Name of data-member#1
prq_    // Name of data-member#2
xyz_    // Name of data-member#3
abcd_   // Name of the abcd_ data-member
prq_    // Name of the prq_ data-member
xyz_    // Name of the xyz_ data-member
abcd_ prq_ xyz_ // Names of all data-members
####################################################
//------------------- Running Results : END ---------------



//#########################################################
//------------------- Compiler & System  ------------------

g++ -v     : gcc version egcs-2.91.57 19980901
             (egcs-1.1 release)

uname -a   : SunOS <nodename> 5.6 Generic_105181-09
             sun4m sparc SUNW,SPARCstation-5

//---------------------------------------------------------

//#########################################################



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]