dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2009
    Posts
    1,220

    how test definitions using Processor directives?

    i'm trying testing if the function was defined:
    Code:
    #ifdef  clsPointer->MouseClick
                 clsPointer->MouseClick();
            #else
                MouseClick();
            #endif // clsPointer
    the function is defined(on these test), but not called... unless i use the '#ifndef'... why?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: how test definitions using Processor directives?

    #ifdef is part of the c++ preprocessor. The preprocessor uses it's own identifiers which are separate from c++ variable names. Preprocessor directives (such as #ifdef) are processed in the source file before the c++ compiler starts. Preprocessor names have the same rules as c++ ones.

    Code:
    #ifdef  clsPointer->MouseClick
        clsPointer->MouseClick();
    #else
        MouseClick();
    #endif // clsPointer
    means if the preprocessor identifier clsPointer has been defined then compile the statement clsPointer->MouseClick() else if the preprocessor identifier clsPointer has not been defined then compile MouseClick().

    Note that clsPointer as used here is not the c++ clsPointer variable!

    As the preprocessor identifier clsPointer has not been defined, then the #else will always be processed.

    Note that it is common to use all capitals for preprocessor identifiers so these can be easily identified in the code.
    Last edited by 2kaud; April 29th, 2018 at 06:00 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,220

    Re: how test definitions using Processor directives?

    for use '#ifdef', i must define another macro?
    because i always get the '#else'.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: how test definitions using Processor directives?

    Code:
    #define USECLS
    ...
    #ifdef USECLS
        clsPointer->MouseClick();    // first usage
    #else
        MouseClick();
    #endif
    
    #undef USECLS
    ...
    #ifdef USECLS
        clsPointer->MouseClick();
    #else
        MouseClick();    // Second
    #endif
    Will compile clsPointer-> for first usage as USECLS is defined and MouseClick() for the second usage as USECLS is not then defined.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,220

    Re: how test definitions using Processor directives?

    i'm sorry, can i use:
    Code:
    #define USECLS 0 

    ?
    (for compare the USECLS value)

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: how test definitions using Processor directives?

    This will give the preprocessor identifier USECLS the value 0. So consider

    Code:
    #define USECLS 1
    ...
    #if USECLS == 1
        clsPointer->MouseClick();    // first usage
    #else
        MouseClick();
    #endif
    
    #define USECLS 0
    ...
    #if USECLS == 1
        clsPointer->MouseClick();
    #else
        MouseClick();    // Second
    #endif
    Last edited by 2kaud; April 29th, 2018 at 07:01 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  7. #7
    Join Date
    Apr 2009
    Posts
    1,220

    Re: how test definitions using Processor directives?

    see the sample:
    Code:
    #define mrMouseClick 0
    template <typename ClassDerived>
    class test
    {
        public:
            ClassDerived *atPointer=nullptr;
        //template<typename ClassDerived>//getting the derived pointer
        test(ClassDerived *clsPointer)
        {
            atPointer=clsPointer;
            #ifdef mrMouseClick==1
                clsPointer->MouseClick(); //error because isn't defined
            #else
                MouseClick();
            #endif // clsPointer
        }
    why can't control the macro?

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: how test definitions using Processor directives?

    Code:
     #if mrMouseClick==1
    Spot the difference!

    See https://msdn.microsoft.com/en-us/library/ew2hz0yd.aspx
    Last edited by 2kaud; April 29th, 2018 at 07:46 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  9. #9
    Join Date
    Apr 2009
    Posts
    1,220

    Re: how test definitions using Processor directives?

    yes... sorry about that.
    but see the complete sample:
    Code:
    //define it to zero:#define mrMouseClick 0
    template <typename ClassDerived>
    class test
    {
        public:
            ClassDerived *atPointer=nullptr;
        //template<typename ClassDerived>//getting the derived pointer
        test(ClassDerived *clsPointer)
        {
            atPointer=clsPointer;
            #if mrMouseClick == 1
                clsPointer->MouseClick();
            #else
                MouseClick();
            #endif // clsPointer
        }
        void call()
        {
            #if mrMouseClick ==1
                atPointer->MouseClick();
            #else
                MouseClick();
            #endif // clsPointer
        }
        void MouseClick(){};
        void Move(){};
    };
    
    
    
    
    //ClassWithEvents(test,FormEvents, b);
    
    
    class InstanceName : public test<InstanceName>
    { // manual expansion of ClassWithEvents
    public:
    	InstanceName() : test(this){;}
    
    
    	~InstanceName(){;}
    
    
    public:
    	void MouseClick();
    	void Move();                       // error 1
    } InstanceName;
    
    
    //redefine it to '1'
    #define mrMouseClick 1
    void InstanceName::MouseClick()
    {
        std::cout << "Mouse click";
    }
    i'm doing a redefinition.. so don't works.
    what you advice me?

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: how test definitions using Processor directives?

    You're trying to use #if etc at runtime - but it only works at compile time! This will never give you polymorphism.

    Why not Wolle's suggestion in post #15 of http://forums.codeguru.com/showthrea...ithout-virtual ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  11. #11
    Join Date
    Apr 2009
    Posts
    1,220

    Re: how test definitions using Processor directives?

    "You're trying to use #if etc at runtime - but it only works at compile time! This will never give you polymorphism."
    i'm sorry, but, in these case, isn't compile time?
    why i'm asking!?! because the '
    clsPointer->MouseClick();' is called only if the function is defined... these happens if i define it.
    if i don't define it, it will use '
    MouseClick();'... it's only i need
    Last edited by Cambalinho; April 29th, 2018 at 09:07 AM.

  12. #12
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: how test definitions using Processor directives?

    clsPointer is a pointer to either a base class or a derived class. The default MouseClick() should be defined in the base class (possibly just to do nothing - but defined) and then for every derived class that needs a different implementation of MouseClick(), define that in the derived class. The code then becomes something like (not tried)

    Code:
    template <typename ClassDerived>
    class test
    {
        public:
            ClassDerived *atPointer=nullptr;
        test(ClassDerived *clsPointer)
        {
            atPointer=clsPointer;
                clsPointer->MouseClick();
        }
        void call()
        {
                atPointer->MouseClick();
        }
    };
    which will work as the base class will have a definition of MouseClick() and any derived classes override the default MouseClick() as needed. So clsPointer will always point (working from derived to base) to a class with a valid defintion.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  13. #13
    Join Date
    Apr 2009
    Posts
    1,220

    Re: how test definitions using Processor directives?

    just doing these:
    Code:
    atPointer->MouseClick();
    (is isn't defined, theres the error )
    the best shot is the lambda, but i'm getting problem on do it:
    Code:
    std::function <void ()> func=atPointer->MouseClick;        
            if(func)
                func();
            else
                MouseClick();
    i simply can't use in these way:
    Code:
    atPointer.MouseClick();
    the compiler will give an error.
    so how can i add it on lambda?
    Last edited by Cambalinho; April 29th, 2018 at 11:17 AM.

  14. #14
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: how test definitions using Processor directives?

    Consider

    Code:
    #include <iostream>
    
    template <typename CBase>
    class test
    {
    	CBase* atPointer = nullptr;
    
    public:
    	test(CBase *clsPointer) : atPointer(clsPointer)
    	{
    		//atPointer->MouseClick();
    	}
    
    	void call()
    	{
    		atPointer->MouseClick();
    	}
    };
    
    class ClassBase {
    public:
    	virtual void MouseClick() {
    		std::cout << "MouseClick NULL implementation" << std::endl;
    	};
    };
    
    class ClassDerivedM : public ClassBase {
    public:
    	void MouseClick() override {
    		std::cout << "MouseClick REAL implementation" << std::endl;
    	}
    };
    
    class ClassDerivedNoM : public ClassBase {
    public:
    };
    
    int main()
    {
    	ClassBase c1;
    	ClassDerivedM c2;
    	ClassDerivedNoM c3;
    
    	test<ClassBase> t1(&c1);
    	test<ClassBase> t2(&c2);
    	test<ClassBase> t3(&c3);
    
    	t1.call();
    	t2.call();
    	t3.call();
    }
    Which displays the expected

    Code:
    MouseClick NULL implementation
    MouseClick REAL implementation
    MouseClick NULL implementation
    as only ClassDerivedM has a non-default implementation for MouseClick().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  15. #15
    Join Date
    Apr 2009
    Posts
    1,220

    Re: how test definitions using Processor directives?

    finally i get what i need:
    Code:
    #include <iostream>
    
    #if defined __GNUC__
        #define EVENT [[gnu::weak]]
    #elif defined __clang__
        #define EVENT [[llvm::weak]]
    #endif
    
    
    #define IS_EVENT_DEFINED(sym) (static_cast<bool>(sym))
    
    
    template <typename CBase>
    class test
    {
        CBase* atPointer = nullptr;
    public:
        test(CBase *clsPointer) : atPointer(clsPointer)
        {
            if (IS_EVENT_DEFINED(&CBase::MouseClick))
                atPointer->MouseClick();
            if (IS_EVENT_DEFINED(&CBase::Move))
                atPointer->Move();
        }
        void call()
        {
            if (IS_EVENT_DEFINED(&CBase::MouseClick))
                atPointer->MouseClick();
            if (IS_EVENT_DEFINED(&CBase::Move))
                atPointer->Move();
        }
    };
    
    
    class InstanceName : public test <InstanceName>
    {
    public:
        InstanceName(): test(this){;}
        ~InstanceName(){;}
    public:
        EVENT void MouseClick();
        EVENT void Move();
    } InstanceName;
    
    
    void InstanceName::MouseClick()
    {
        std::cout << "Mouse click\n";
    }
    
    
    void InstanceName::Move()
    {
       std::cout << "Move\n";
    }
    
    
    int main()
    {
        InstanceName.call();
        std::cin.get();
        return 0;
    }
    but i need help with 2 things:
    1 - can i convert the code for use virtual functions(but continue with EVENT macro)?
    i tried without success
    Code:
    template <typename CBase>class test
    {
        CBase* atPointer = nullptr;
        virtual void MouseClick(){};
        virtual void Move(){};
    public:
        test(CBase *clsPointer) : atPointer(clsPointer)
        {
            //if (IS_EVENT_DEFINED(&CBase::MouseClick))
                MouseClick();
            //if (IS_EVENT_DEFINED(&CBase::Move))
                Move();
        }
        void call()
        {
            //if (IS_EVENT_DEFINED(&CBase::MouseClick))
               MouseClick();
            //if (IS_EVENT_DEFINED(&CBase::Move))
                Move();
        }
    };
    
    
    class InstanceName : public test <InstanceName>
    {
    public:
        InstanceName(): test(this){;}
        ~InstanceName(){;}
    public:
        EVENT void MouseClick();
        EVENT void Move();
    } InstanceName;
    the Move() isn't called

    2 - the VS have 'weak' for i add on my macro:
    Code:
    #if defined __GNUC__
        #define EVENT [[gnu::weak]]
    #elif defined __clang__
        #define EVENT [[llvm::weak]]
    #endif
    ???
    (these macro isn't prepared for use it on Visual Studio)
    Last edited by Cambalinho; May 11th, 2018 at 05:51 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)