If i have a sample code like this:
void func_1 {
.......
func_2;
}
func_2{
.......
}
I need to declare a function identifier for func_2 so that the code could run how do i do that?
If func_2 won't call func_1, then you can just reorder them:
void func_2()
{
}
void func_1()
{
// ...
func_2();
}
If they both call each other, then you can declare like so:
void func2();
void func1()
{
// ...
func2();
}
void func2()
{
// ...
func1();
}
void func_2 ();
void func_1 ()
{
...
}
void func_2 ()
{
...
}
Related
I'm trying to refactor some code by facotirising duplicate code.
Let's imagine I end up with a class like:
class A {
public:
void method1()
{
do_work(true);
}
void method2() const
{
do_work(false);
}
private:
void do_work(bool flag) {
...
if(flag)
this->data = 0;
}
}
However this won't compile because do_work is not const, even though calling this method with flag = false is const.
Is there a way to "fix" this without splitting again the implementations?
do_work is poorly refactored, as a function that is "sometimes const".
Separate out the const and non-const logic.
class A {
void method1()
{
do_work();
this->data = 0;
}
void method2() const
{
do_work();
}
private:
void do_work() const {
...
}
}
Is this code legal? It compiles but I'm wondering what happens with the return value. Undefined behavior?
class Foo {
public:
void test1() {
}
auto test() -> decltype(test1()) {
return test1(); //<---return void here!
}
};
The code is legal. auto deduces to void and a void function can return another void function. A void function can even
return static_cast<void>("I'm a void");
It's legal, but you can't, for example, assign the result to a variable. [1]
class Foo {
public:
void test1() {
}
auto test() -> decltype(test1()) {
return test1(); //<---return void here!
}
};
int main() {
Foo foo;
auto x = foo.test(); //<---compile error here
}
[1] https://godbolt.org/z/YGAtdJ
I want to pass a callback function that has a parameter:
class foo1{
foo1(void (*callback)(float));
};
foo1::foo1(void (*callback)(float)){
//excecute the callback at some point
}
float foo2(){
return 1.1;
}
void foo3(float f){
//do stuff with f
return;
}
int main(){
void (*p3)(float);
//p3 is a pointer to a function that returns void and has a single float as input
p3 = &foo3(foo2());
//p3 now points to foo3 wich fits the requirements. But it does not make sence to give that pointer an argument.
foo1(p3);
return 0;
}
There are several errors and
I understand that this does not make sence. (See comments in the code) But I don't know how to do it corretly. I want to pass a function as callback that has the input value of foo2.
You can do something more like this instead:
class foo1 {
foo1(void (*callback)(float));
};
float foo2();
foo1::foo1(void (*callback)(float)) {
//excecute the callback at some point
callback(foo2());
}
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
int main() {
void (*p3)(float);
p3 = &foo3;
foo1(p3);
// or simply:
// foo1(&foo3);
return 0;
}
If you do not want foo1() passing a parameter value to the callback, then don't declare the callback with an input parameter to begin with, use another callback function that calls the intended callback function:
class foo1 {
foo1(void (*callback)());
};
foo1::foo1(void (*callback)()) {
//excecute the callback at some point
callback();
}
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
void foo4() {
foo3(foo2());
}
int main() {
void (*p4)();
p4 = &foo4;
foo1(p4);
// or simply:
// foo1(&foo4);
return 0;
}
Or, in C++11 and later, you can use a lambda:
class foo1 {
template<class T>
foo1(T callback) {
//excecute the callback at some point
callback();
}
};
Or:
#include <functional>
class foo1 {
foo1(std::function<void()> callback) {
//excecute the callback at some point
callback();
}
};
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
int main() {
foo1([](){ foo3(foo2()); });
return 0;
}
Or, you can use std::bind():
class foo1 {
template <typename T>
foo1(T callback) {
//excecute the callback at some point
callback();
}
};
or:
#include <functional>
class foo1 {
foo1(std::function<void()> callback) {
//excecute the callback at some point
callback();
}
};
#include <functional>
float foo2() {
return 1.1;
}
void foo3(float f) {
//do stuff with f
}
int main() {
foo1(std::bind(foo3, foo2()));
return 0;
}
You can use a lambda to do that.
Something along this way should work:
struct foo1{
template<typename F>
foo1(F f) {
//excecute the callback at some point
f();
}
};
float foo2(){
return 1.1;
}
void foo3(float){
//do stuff with f
return;
}
int main(){
foo1([param{foo2()}](){ foo3(param); });
}
Consider this expression:
[param{foo2()}](){ foo3(param); }
It creates a callable object having type void(void), that is what you expected by applying the result of the execution of foo2 at the first argument of foo3 (right?).
That's why you can simply invoke it as f() within the constructor of foo1.
I've never used function pointers before and I'm having some trouble getting my code to work. This is what I have
TestClass.h:
class TestClass
{
public:
void function1();
void function2();
void function3(void (*funcPtr)(void))
void function4();
};
TestClass.cpp
void TestClass::function1()
{
//CODE
}
void TestClass::function2()
{
//CODE
}
void TestClass::function3(void (*funcPtr)(void))
{
//CODE
funcPtr();
//CODE
}
void TestClass::function4()
{
function3(function1);
function3(function2);
}
This give me the error
"nonstandard form for taking the address of a member function
I tried to add TestClass:: infront of the *funcPtr but that gives me even more errors
With member function pointer, it should be something like:
void TestClass::function3(void (TestClass::*funcPtr)())
{
//CODE
(this->*funcPtr)();
//CODE
}
void TestClass::function4();
{
function3(&TestClass::function1);
function3(&TestClass::function2);
}
With function pointer
class TestClass
{
public:
static void function1(); // static added
static void function2(); // static added
void function3(void (*funcPtr)(void))
void function4();
};
void TestClass::function3(void (*funcPtr)())
{
//CODE
funcPtr();
//CODE
}
void TestClass::function4();
{
function3(&TestClass::function1);
function3(&TestClass::function2);
}
I suggest you to use std::bind and std::function, which provide a better readability and more checking for you
http://en.cppreference.com/w/cpp/utility/functional/bind
#include <functional>
void TestClass::function3( std::function<void (void)> funcPtr )
{
//CODE
funcPtr();
//CODE
}
void TestClass::function4()
{
function3( std::bind(&TestClass::function1, this) );
function3( std::bind(&TestClass::function2, this) );
}
How can I pass a function as an argument and then execute it. I'm trying to do something like this:
class Foo{
private:
void (*external);
public:
Foo(void (*function)()){ *external = *function; }
~Foo(){ }
bool Execute(){
*external(); // Somehow execute 'external' which does the same thing with 'function'
return true
}
};
void pFnc(){
printf("test");
}
int main(){
Foo foo = Foo(&pFnc);
foo.Execute();
return 0;
}
This is not working of course.
You were close.
class Foo
{
public:
typedef void(*FN)(void);
Foo(FN fn) : fn_(fn) {};
bool Execute()
{
fn_();
return true;
}
FN fn_;
};
void pFunc(){
printf("test");
}
int main()
{
Foo foo(&pFunc);
foo.Execute();
}
Try:
void (*external)();
Your original declaration is a pointer to void, not a pointer to a function returning void.
Set it with
external = function;
and execute with
external();
Also, external has to be declared as a function pointer void (*external)(). Otherwise, you have to cast between function- and void-pointer.