Propose to the user to rewrite functions with lambda - c++

I'm doing tests to understand lambdas. I am trying to offer to the users the ability to rewrite a function directly in the main function. Let me explain:
#include <iostream>
using namespace std;
class A {
public:
virtual bool execute() {};
};
class B : public A {
public:
bool execute() { cout << "Execute in B" << endl; }
};
int main() {
B newB;
newB.execute();
newB.execute() { cout << "Execute in New B" << endl; } ;
newB.execute();
return 0;
}
This source code doesn't work because it's illegal to rewrite a function like that. What would be for you, the best way to rewrite a function like that in C ++ 14? With lambda? Without lambda?
I want do like in Javascript, overloading a function like that: newB.somefunction = function(...) { ... };. I want the function to be written in source code by a user of my Library. In a way a callback function.
my question is the following: How to write a callback function or Lambda expressions to rewrite a method outside a class/object?
Solution proposed by Exagon with variable :
#include <iostream>
#include <functional>
class B {
public:
int global=0;
std::function<void()> execute{
[](){
std::cout << "Hello World" << std::endl;
}
};
};
int main() {
B newB;
newB.execute();
newB.execute();
newB.execute = [newB](){std::cout << newB.global << " = FOOBAR\n";};
newB.execute();
return 0;
}

You can do this using std::function from the functional header.
Then make a std::function member and create a setter for this member.
the execute member function need to call this std::function member.
you can pass a lambda into the setter method.
here is my approach:
#include <iostream>
#include <functional>
class B {
public:
void execute() {_f();}
void setFunction(std::function<void()> f){ _f = f;}
private:
std::function<void()> _f{[](){std::cout << "Hello World" << std::endl;}};
};
int main() {
B newB;
newB.execute();
newB.execute();
newB.setFunction([](){std::cout << "FOOBAR\n";});
newB.execute();
return 0;
}
the output is:
Hello World
Hello World
FOOBAR
Since you are after something "JavaScript-like" you could do it like this:
#include <iostream>
#include <functional>
class B {
public:
std::function<void()> execute{
[](){
std::cout << "Hello World" << std::endl;
}
};
};
int main() {
B newB;
newB.execute();
newB.execute();
newB.execute = [](){std::cout << "FOOBAR\n";};
newB.execute();
return 0;
}
Which has the same output.
here is a live demo

Related

C++ non-generic class in template

I would like to know how to make a template with an own class:
#include <iostream>
using namespace std;
template<C cc> void A()
{
cout << cc.l << endl;
}
int main()
{
C cc;
A<cc>();
}
class C
{
public:
int l = 10;
};
But it doesn't work, so how to use that class, like a non-generic class parameter, like here:
#include <iostream>
using namespace std;
template<int i> void A()
{
cout << i << endl;
}
int main()
{
A<100>();
}
You can do it as shown below with C++20(&onwards):
//moved definition of C before defining function template `A`
struct C
{
int l = 10;
};
template<C cc> void A()
{
cout << cc.l << endl;
}
int main()
{
//--vvvvvvvvv--------->constexpr added here
constexpr C cc;
A<cc>();
}
Working demo
Two changes have been made:
As template arguments must be compile time constant, constexpr is used.
The definition of C is moved before the definition of function template.

Constructor called outside of main

I saw this question about constructors and I don't understand why the variable a is calling the constructor.
I thought it wass an error because the variable declaration is out of main without stating global before the name of it and they only wrote a; without the class name before it. How does the compiler know that the variable is of type Test?
#include <iostream>
using namespace std;
class Test
{
public:
Test() { cout << "Hello from Test() "; }
} a;
int main()
{
cout << "Main Started ";
return 0;
}
The answer for the output was - "Hello from Test() Main Started".
As you might know, we have two ways to create an object from the Test class:
first, in the main:
class Test
{
public:
Test() { cout << "Hello from Test() "; }
};
int main()
{
Test a;
cout << "Main Started ";
return 0;
}
Or before starting main in the definition of your class (like your code):
class Test
{
public:
Test() { cout << "Hello from Test() "; }
} a;
int main()
{
cout << "Main Started ";
return 0;
}
In both cases, you are calling the constructor of the Test class once you make an object from it. In the main or before it.

Does std::future support polymorphism?

Does std::future in c++ support polymorphism?
So, if to store child_class in future<parent_class>, can I after get it after by dynamic_cast<child_class>?
Providing you use a reference or a pointer (probably obvious since it'll fail to compile otherwise)... Yes.
#include <iostream>
#include <future>
using namespace std;
struct Parent {
virtual void a() { cout << "I am parent"; }
};
struct Child : Parent {
virtual void a() { cout << "I am child"; }
};
Child g_c; //just some global for the purposes of the example
int main() {
std::future<Parent&> p = async(launch::async, []() -> Parent& { return g_c; });
auto c = dynamic_cast<Child&>(p.get());
c.a();
return 0;
}
code result here: http://ideone.com/4Qmjvc

destructor ignore string assignment

I have created a program in C++ for a class, and one of the requirements is to output a string when certain parts of the program have been called. For most of these I have simply assigned a string to a member variable and then outputted that variable. I wanted to know is it possible for me to assign the string in a destructor and then output that string? When I try it, it outputs nothing.
ie:
Class
private:
string output;
~Class {
output = "destructor has fired!";
}
int main(){
cout << class.message;
}
This is pseudocode so please ignore syntax mistakes/missing pieces.
It certainly is possible to output a message in the destructor, to know that it has fired, and one way to do it is this...
#include <iostream>
#include <string>
using namespace std;
class C{
string output; // by default private
public:
C(){}
~C() { cout << output << endl; }
void setString(const string& s) {
output = s;
}
};
int main()
{
{
C s;
s.setString("Destructor has fired");
}
return 0;
}
If I understand your question right, this is what you are expected to do. Note: no member variable, direct calls to std::cout.
#include <iostream>
#include <string>
using namespace std;
class C{
public:
C() {
cout << "C ctor" << endl;
}
~C() {
cout << "C dtor" << endl;
}
};
int main()
{
{
C s;
}
return 0;
}

C++ Function Pointer as Argument

I have tried multiple google searches and help guides, but I'm out of ideas on this one. I have a function pointer that I am using as an argument for another function. Both functions are within the same class. However, I keep getting type conversion errors. I'm sure this is just a syntax problem, but I can't understand what the correct syntax is. Here is a simplified version of my code:
Header File
#ifndef T_H
#define T_H
#include <iostream>
#include <complex>
namespace test
{
class T
{
public:
T();
double Sum(std::complex<double> (*arg1)(void), int from, int to);
int i;
std::complex<double> func();
void run();
};
}
#endif // T_H
Source File
#include "t.h"
using namespace test;
using namespace std;
//-----------------------------------------------------------------------
T::T()
{
}
//-----------------------------------------------------------------------
double T::Sum(complex<double>(*arg1)(void), int from, int to)
{
complex<double> out(0,0);
for (i = from; i <= to; i++)
{
out += arg1();
cout << "i = " << i << ", out = " << out.real() << endl;
}
return out.real();
}
//-----------------------------------------------------------------------
std::complex<double> T::func(){
complex<double> out(i,0);
return out;
}
//-----------------------------------------------------------------------
void T::run()
{
Sum(&test::T::func, 0, 10);
}
Whenever I try to compile, I get the following error:
no matching function for call to 'test::T::Sum(std::complex<double> (test::T::*)(),int,int)'
note: no known conversion for argument 1 from 'std::complex<double> (test::T::*)()' to 'std::complex<double>(*)()'
Any advice appreciated. Or at least a link to a thorough site on how to use function pointers. I am using Qt Creator 2.6.2, compiling with GCC.
Your Sum function expects pointer to a function. And then you try to call it with a pointer to a member function. Learn about pointers to members.
The code itself is a bit messy, I'll only correct the grammer to make it work.
firstly, you shall change the function prototype from
double Sum(std::complex<double> (*arg1)(void), int from, int to);
to
double Sum(std::complex<double> (T::*arg1)(void), int from, int to);
Meaning that it is a pointer to class T's member.
Then, when calling the function, you cant just arg1(),
for (i = from; i <= to; i++)
{
out += arg1();
cout << "i = " << i << ", out = " << out.real() << endl;
}
you have to use (this->*arg1)();
for (i = from; i <= to; i++)
{
out += (this->*arg1)();
cout << "i = " << i << ", out = " << out.real() << endl;
}
How to pass functions as arguments in C++? In general, use a template, unless you have very compelling reasons not do it.
template<typename Func>
void f(Func func) {
func(); // call
}
On the call side, you can now throw in a certain amount of objects (not just pointers to functions):
Functors;
struct MyFunc {
void operator()() const {
// do stuff
}
};
// use:
f(MyFunc());
Plain functions:
void foo() {}
// use
f(&foo) {}
Member functions:
struct X {
void foo() {}
};
// call foo on x
#include <functional>
X x;
func(std::bind(&X::foo, x));
Lambdas:
func([](){});
If you really want a compiled function and not a template, use std::function:
void ff(std::function<void(void)> func) {
func();
}