Thread: using more about macros

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    using more about macros

    the question is more geral.
    because i need create 1 macro that i don't know how, but i belive that it's possible...
    but for understand what i need, heres a sample:
    Code:
    class test
    {
        public:
              void test2(); //it's a prototype
    };
    
    classwithprototypes(test, testbase);//classwithprototypes it's the new macro
    so the classwithprototypes macro must convert the code to:
    Code:
    class testbase:public test
    {
        public:
              void test2(); //ok... here we use 1 function.. but imagine with 2 or more
    };
    so the best question for start would be: how can i get the functions prototypes using macros?

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    heres my actual code:
    Code:
    #include <iostream>class a
    {
        public:
        int b;
        void hello(); //adding these on macro
    };
    
    
    #define ClassWithEvents(DerivedClass,InstanceName) \
                    class InstanceName : public DerivedClass {}InstanceName;
    
    
    ClassWithEvents(a, c);
    ClassWithEvents(a, d);
    using namespace std;
    int main()
    {
        c.b=10;
        d.b=20;
        cout << c.b << d.b;
        return 0;
    }
    see the class 'a':
    Code:
    class a{
        public:
        int b;
        void hello(); //adding these on macro
    };
    and now see the macro:
    Code:
    #define ClassWithEvents(DerivedClass,InstanceName) \
                    class InstanceName : public DerivedClass {}InstanceName;
    using macros, how can i add the functions prototype between blocks\breaks?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You do realise that since hello is not virtual, what you are trying to do is to overload rather than override the member functions from the base class?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    heres my actual code:
    Code:
    #include <iostream>
    
    //create the class, from base class and the functions prototype, that don't need instances
    //the '...' and '__VA_ARGS__' is for constructor parameters:
    #define ClassWithEvents(DerivedClass, MacroEventsUsed, InstanceName, ...) \
                    class InstanceName : public DerivedClass {public: InstanceName():DerivedClass(__VA_ARGS__){;} \
                    ~InstanceName(){;}  MacroEventsUsed}InstanceName;
    
    
    //macro that have the function prototypes:
    #define FormEvents public: void MouseClick(); void Move();
    
    
    //heres an entire sample:
    class test
    {
        public:
            int inttest=0;
        test()
        {
            MouseClick();
            Move();
        }
        virtual void MouseClick(){} ;
        virtual void Move(){};
    };
    
    
    //creating an 'instance':
    ClassWithEvents(test,FormEvents, b);//yes i can create anothers like 'b'
    
    
    //define the functions prototype:
    void b::MouseClick()
    {
        std::cout << "Mouse click";
    }
    
    
    //but don't need to be all prototype functions:
    /*void b::Move()
    {
       std::cout << "Move";
    }*/
    
    
    int main()
    {
        b.inttest=100;
        b.MouseClick();
        std::cout << b.inttest;
        std::cin.get();
        return 0;
    }
    the Move() isn't defined, so i have 2 errors:
    1 - "obj\Debug\testingevents.o:testingevents.cpp.rdata$_ZTV1b[__ZTV1b]+0xc)||undefined reference to `b::Move()'|"
    2 - ||error: ld returned 1 exit status|

    how can i fix these error?
    what i need is the FormEvents macro been universal and then just define what i need. how can i do it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with macros
    By ManikSinghal in forum C Programming
    Replies: 1
    Last Post: 10-14-2013, 12:05 AM
  2. C macros
    By rendezed in forum C Programming
    Replies: 4
    Last Post: 01-26-2011, 08:36 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  4. macros
    By rpc2005 in forum C Programming
    Replies: 23
    Last Post: 06-14-2005, 08:56 AM
  5. macros
    By Chaplin27 in forum C++ Programming
    Replies: 7
    Last Post: 03-02-2005, 01:06 PM

Tags for this Thread