LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   fun with templates, typedefs, and , (https://www.linuxquestions.org/questions/programming-9/fun-with-templates-typedefs-and-865281/)

ta0kira 02-27-2011 04:08 AM

fun with templates, typedefs, and ,
 
Hello, everyone! I thought I'd share this bit of obscure C++ I came up with because it entertained me.
Code:

#include <iostream>


struct empty_struct
{
        typedef void print_type() const;
        virtual print_type printme = 0;
};


template <class Type>
void print_it(const Type &oObject, typename Type::print_type Type:: *mMembers[])
{ if (mMembers) while (*mMembers) (oObject.**mMembers++)(); }


class derived_struct : public empty_struct
{
public:
        print_type derived_struct:: *operator , (bool rReally) const
        { return rReally? &derived_struct::showme : NULL; }

private:
        void showme() const
        { std::cout << "showed!\n"; }

        void printme() const
        { std::cout << "printed!\n"; }
};


int main()
{
        derived_struct object;

        empty_struct::print_type derived_struct:: *members[] =
          { &empty_struct::printme, (object, true), (object, false) };

        print_it(object, members);
}

I personally think it's silly that this can be done, but I also appreciate that it can be done in principle. If only I could figure out how to define printme and showme using the typedef!
Kevin Barry


All times are GMT -5. The time now is 09:07 PM.