Results 1 to 6 of 6

Thread: Learning C++, looking at function pointers. Is this Good?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    15

    Learning C++, looking at function pointers. Is this Good?

    Hi all.
    After playing with visual basic and c# for a few years I am now trying to teach myself a little c++.

    I am taking a little time to look at function pointers and have this so far.

    Code:
    #include <iostream>
    
    
    class Test {
    
    private:
    	void (*ExternalFunction)() = nullptr;
    
    public:
    	void CallExternalFunction() {
    		if (ExternalFunction != nullptr) {
    			ExternalFunction();
    		}
    		else
    		{
    			std::cout << "No function called. No one says Hi." << std::endl;
    		}
    	}
    
    	void SetExternalFunction( void (*FunctioToBeCalled)()) {
    		ExternalFunction = FunctioToBeCalled;
    
    
    	}
    
    	Test& operator+= (void (*ToBeCalled)()) {
    		ExternalFunction = ToBeCalled;
    		return *this;
    	}
    };
    
    
    void  Func1() {
    	std::cout << "Func1 says hello!" << std::endl;
    }
    
    void  Func2() {
    	std::cout << "Func2 says hello!" << std::endl;
    }
    int main() {
    
    	Test myTest;
    	myTest += Func1;
    	myTest.CallExternalFunction();
    	myTest.SetExternalFunction(Func2);
    	myTest.CallExternalFunction();
    	myTest.SetExternalFunction(nullptr);
    	myTest.CallExternalFunction();
    	return 0;
    }
    This code has no purpose other than learning. Any suggestions on what I have missed?

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Learning C++, looking at function pointers. Is this Good?

    Why do you think you have missed something? I wonder whether the question you have asked is not the actual question you want answered.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    15

    Re: Learning C++, looking at function pointers. Is this Good?

    jmcilhinney - I feel you missed your calling.

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Learning C++, looking at function pointers. Is this Good?

    Easier like this:

    Code:
    #include <iostream>
    
    using Func = void(*)();
    
    class Test {
    	Func ExternalFunction {};
    
    public:
    	Test(Func f = nullptr) : ExternalFunction(f) {};
    
    	void CallExternalFunction() {
    		if (ExternalFunction)
    			ExternalFunction();
    		else
    			std::cout << "No function called. No one says Hi.\n";
    	}
    
    	void SetExternalFunction(Func FunctioToBeCalled = nullptr) {
    		ExternalFunction = FunctioToBeCalled;
    	}
    };
    
    void Func1() {
    	std::cout << "Func1 says hello!\n";
    }
    
    void  Func2() {
    	std::cout << "Func2 says hello!\n";
    }
    
    int main() {
    	Test myTest { Func1 };
    
    	myTest.CallExternalFunction();
    	myTest.SetExternalFunction(Func2);
    	myTest.CallExternalFunction();
    	myTest.SetExternalFunction();
    	myTest.CallExternalFunction();
    	myTest = Func1;
    	myTest.CallExternalFunction();
    }
    Code:
    Func1 says hello!
    Func2 says hello!
    No function called. No one says Hi.
    Last edited by 2kaud; Aug 21st, 2022 at 04:38 AM.
    All advice is offered in good faith only. 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/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    15

    Re: Learning C++, looking at function pointers. Is this Good?

    Quote Originally Posted by jmcilhinney View Post
    Why do you think you have missed something? I wonder whether the question you have asked is not the actual question you want answered.
    Ignoring my other facetious comment. The question I suppose is how would I refactor this code.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    15

    Re: Learning C++, looking at function pointers. Is this Good?

    Quote Originally Posted by 2kaud View Post
    Easier like this:

    Code:
    #include <iostream>
    
    using Func = void(*)();
    
    class Test {
    	Func ExternalFunction {};
    
    public:
    	Test(Func f = nullptr) : ExternalFunction(f) {};
    
    	void CallExternalFunction() {
    		if (ExternalFunction)
    			ExternalFunction();
    		else
    			std::cout << "No function called. No one says Hi.\n";
    	}
    
    	void SetExternalFunction(Func FunctioToBeCalled = nullptr) {
    		ExternalFunction = FunctioToBeCalled;
    	}
    };
    
    void Func1() {
    	std::cout << "Func1 says hello!\n";
    }
    
    void  Func2() {
    	std::cout << "Func2 says hello!\n";
    }
    
    int main() {
    	Test myTest { Func1 };
    
    	myTest.CallExternalFunction();
    	myTest.SetExternalFunction(Func2);
    	myTest.CallExternalFunction();
    	myTest.SetExternalFunction();
    	myTest.CallExternalFunction();
    	myTest = Func1;
    	myTest.CallExternalFunction();
    }
    Code:
    Func1 says hello!
    Func2 says hello!
    No function called. No one says Hi.
    I will look at 'using' and take onboard class parameter intialisation.

Posting Permissions

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



Click Here to Expand Forum to Full Width