-
Aug 20th, 2022, 09:02 PM
#1
Thread Starter
New Member
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
-
Aug 20th, 2022, 10:01 PM
#2
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.
-
Aug 20th, 2022, 10:29 PM
#3
Thread Starter
New Member
Re: Learning C++, looking at function pointers. Is this Good?
jmcilhinney - I feel you missed your calling.
-
Aug 21st, 2022, 04:32 AM
#4
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)
-
Aug 21st, 2022, 01:49 PM
#5
Thread Starter
New Member
Re: Learning C++, looking at function pointers. Is this Good?
Originally Posted by jmcilhinney
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.
-
Aug 21st, 2022, 01:55 PM
#6
Thread Starter
New Member
Re: Learning C++, looking at function pointers. Is this Good?
Originally Posted by 2kaud
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|