Is there a way to use an = type assignment instead of this syntax:
void triggerAttack() { adsr.triggerAttack(); }
I was hoping to do something like:
void triggerAttack() = adsr.triggerAttack
std::function<void()> triggerAttack = adsr.triggerAttack
void(*triggerAttack)() = adsr.triggerAttack
but nothing compiles!
example code:
class LinearADSR
{
public:
void triggerAttack() { }
};
class JerobeamBlubb : public gen
{
public:
void triggerAttack() { adsr.triggerAttack(); }
protected:
LinearADSR adsr;
};
In general, a member function pointer differs from usual pointers, since it has to be used with an instance of its class.
So change your code to this:
class LinearADSR
{
public:
void triggerAttack() { }
};
class JerobeamBlubb
{
public:
void (LinearADSR::*triggerAttack)();
protected:
LinearADSR adsr;
};
int main()
{
JerobeamBlubb a;
a.triggerAttack = &LinearADSR::triggerAttack;
}
About your failed attempts:
void triggerAttack() = adsr.triggerAttack; is invalid syntax
std::function<void()> triggerAttack = adsr.triggerAttack fails because triggerAttack is a member function, and not a usual function. You need an instance of its class as I explained before.
void(*triggerAttack)() = adsr.triggerAttack fails for the same reason as above.
Related
Say I have this class:
class Object {
public:
int x;
int y;
void update(SDL_Event);
void start();
};
I want to be able to make start and update change behavior from instance to instance.
I have tried using function pointers, like so:
class Object {
public:
int x;
int y;
void (*update)(SDL_Event);
void (*start)(void);
};
void teststart() {
x++;
return;
}
And in my main(), I do:
testlevel.objs[1].start = teststart;
But then, I cannot reference variables from inside the class.
I am sure there is something obvious I am overlooking, but searching online yields no results for when the pointer is in the class.
Maybe function pointers are not the right answer?
You would need to pass your functions a pointer/reference to the Object instance, eg:
class Object {
public:
int x;
int y;
void (*doupdate)(Object*, SDL_Event);
void (*dostart)(Object*);
void update(SDL_Event event) { if (doupdate) doupdate(this, event); }
void start() { if (dostart) dostart(this); }
};
void teststart(Object* obj) {
obj->x++;
}
void testupdate(Object* obj, SDL_Event event) {
// update obj as needed...
}
Object &obj = testlevel.objs[1];
obj.dostart = teststart;
obj.doupdate = testupdate;
Otherwise, use std::function with capturing lambdas instead:
#include <functional>
class Object {
public:
int x;
int y;
std::function<void(SDL_Event)> update;
std::function<void()> start;
};
Object *obj = &(testlevel.objs[1]);
obj->start = [obj](){
obj->x++;
};
obj->update = [obj](SDL_Event event){
// update obj as needed...
};
I would like to have the following class setup in a program:
A class that implements a buffer. This buffer, when full, would spawn a thread that makes a callback to handle what to do with the full buffer.
A base class template that includes a buffer object. Implements the callback function, which makes a call to a virtual function defined in a derived class.
A derived class that inherits from base class and implements what to do with the data.
First, the minimal reproducible example:
#include <vector>
#include <iostream>
#include <thread>
template <typename T>
class CallbackBuffer
{
public:
std::vector<T> buffer;
void (*callback)(std::vector<T>);
std::thread writerThread;
CallbackBuffer(int bufferSize = 10)
{
buffer.resize(bufferSize);
}
void setCallback(void (*cb)(std::vector<T>))
{
callback = cb;
}
void writeCall()
{
writerThread = std::thread(callback, buffer);
}
};
template <typename T>
class Base
{
public:
CallbackBuffer<T> buffer;
Base()
{
buffer.setCallback(bufferHandler);
}
void bufferHandler(std::vector<T> v)
{
for(auto &i : v)
{
write(i);
}
}
virtual void write(T i) = 0;
};
class Derived : public Base<int>
{
public:
Derived()
{
}
void write(int i)
{
std::cout << i << std::endl;
}
};
int main()
{
Derived d;
return 0;
}
I'm getting the following compiler error:
error: invalid use of non-static member function ‘void Base<T>::bufferHandler(std::vector<T>) [with T = int]’
So the compiler needs bufferHandler to be static, but if I did that, then I would not have access to the object's members. Is there a way to sort this, or just a horrible idea?
You are passing the class member function so you need to have in your CallbackBuffer class something like:
void (Base<T>::*callback)(std::vector<T>);
// ...
void setCallback(void (Base<T>::*cb)(std::vector<T>)) {
callback = cb;
}
and in Base class:
Base() {
buffer.setCallback(&Base<T>::bufferHandler);
}
Demo
Member function pointers have different type than regular functions, that's why your code does not work. What you may want is using std::function instead of raw pointers:
#include <functional>
//...
std::function<void(std::vector<T>)> callback;
// ...
void setCallback(const std::function<void(std::vector<T>)>& cb) {
callback = cb;
}
and pass it like that:
Base() {
buffer.setCallback([this](auto& vec){ this->bufferHandler(vec); });
}
IMHO this is much more readable and flexible than passing around member pointers
I have the following code:
#include <iostream>
class First {
public:
Second* x;
void make_value(Second* sec_);
First() {
// Initialization
}
};
class Second {
public:
First* y;
Second() {
// Initialization
}
};
void First::make_value(Second* sec_) {
x = sec_;
}
void main() {
fir = new First();
sec = new Second();
fir->make_value(sec);
}
The two classes each have a member variable of the other class, which does not work for obvious reasons.
My question is whether or not there is a way to late-initialize variable x after class Second has been initialized. If not, what alternatives are there?
For any uses where the compiler doesn't need the definition of a class, a forward declaration will suffice. Pointers and references to types do not require a definition.
class Second; // forward declare
class First {
public:
Second* x;
void make_value(Second* sec_);
First() {
// Initialization
}
};
class Second {
public:
First* y;
Second() {
// Initialization
}
};
void First::make_value(Second* sec_) {
x = sec_;
}
Is it possible to pass this by default ?
Here is what I currently have
class A
{
public:
template<typename T>
void dowithT(T t) {}
};
class B
{
public:
A a;
B()
{
//Calling 'dowithT' with 'this'
a.dowithT(this);
}
};
This function requires passing this from the caller of the function every time. So I wondered if there is a way to encapsulate this task, so that you don't need to pass this to dowithT.
I tried to do something like this:
class A
{
public:
// '= this' doesn't compile
template<typename T>
void dowithT(T t = this) {}
};
class B
{
public:
A a;
B()
{
//Calling 'dowithT' without 'this'
a.dowithT();
}
};
Unfortunately, I can't use templates, so my first solution isn't an option.
Is this possible?
Edit: I gave a concrete answer with my own implementation below. Also with a few mor deatils of what I wanted in the end.
TL;DR No, this is not possible.
this is not the same type in every class, you can't generalize it, so no, not possible.
Additionally, what would this be if doWithT() was called from a non-member function? nullptr?
That's why it isn't possible. You have to use a template.
Instead of B having a member of type A, it can inherit from A, and use something like the "curiously recurring template pattern."
If you cannot make class A a template, you can still do it like so:
class A
{
protected:
template <class T>
void dowithT()
{
T* callerthis = static_cast<T*>(this);
// callerthis is the "this" pointer for the inheriting object
cout << "Foo";
}
};
class B : public A
{
public:
B()
{
dowithT<B>();
// Or A::dowithT<B>();
}
};
dowithT() must only be called by an inheriting class (hence I made it protected), with the template parameter the caller's own type, or you'll break everything.
You may achieve exactly what you want by using a private mixin class to provide the dowithT method that takes no arguments:
#include <iostream>
#include <typeinfo>
class A
{
public:
template<typename T>
void dowithT(T* t) {
std::cout << "Hello, World" << typeid(*t).name() << std::endl;
}
};
template<class Owner>
struct calls_a
{
void dowithT()
{
auto p = static_cast<Owner*>(this);
p->a.dowithT(p);
}
};
class B
: private calls_a<B>
{
friend calls_a<B>;
A a;
public:
B()
{
//Calling 'dowithT' with 'this'
dowithT();
}
};
int main()
{
B b;
}
No, it is not possible. There is nothing really special about this when used as an argument to a function taking T* (template or not), it's just a pointer like any other.
this A is different from this B. In your first code, this refers to the caller, while in the second this refers to the callee. Thus what you want to do isnt really possible.
Here's one possibility, which might, or might not suit your needs:
template<typename T>
class A
{
public:
A(T t) : t(t) {}
void dowithT()
{
cout << "Foo";
}
private:
T t;
};
class B
{
public:
A<B*> a;
B() : a(this)
{
a.dowithT();
}
};
You could use a private method in class B that acts as a relay, and use the constant nullptr as a special value for this, if you want to be able to pass other values:
class B
{
public:
A a;
B()
{
//Calling 'dowithT' with 'this'
innerdo();
}
private:
void innerdo(B *p = nullptr) {
if (p == nullptr) p = this;
a.dowithT(p);
}
};
If you only need to pass this it is even simpler
void innerdo() {
a.dowithT(this);
}
After trying out various things you mentioned, I'd like to give my answer/solution to the problem myself to clarify some details:
#include <iostream>
using namespace std;
#include <functional>
template <typename CallerType>
class AFunctionConstructor{
private:
virtual void abstr()
{}
public:
typedef void(CallerType::*CallerTypeFunc)();
function<void()>* constructFunction(CallerTypeFunc func)
{
CallerType* newMe = dynamic_cast<CallerType*> (this);
return new function<void()>(std::bind(func,newMe));
}
};
class A : public function<void()>
{
protected:
public:
A();
A(function<void()>* func) : function<void()>(*func)
{}
};
// now create ressource classes
// they provide functions to be called via an object of class A
class B : public AFunctionConstructor<B>
{
void foo()
{
cout << "Foo";
}
public:
A a;
B() : a(constructFunction(&B::foo)) {}
};
class C : public AFunctionConstructor < C >
{
void bar()
{
cout << "Bar";
}
public:
A a;
C() : a(constructFunction(&C::bar)) {}
};
int main()
{
B b;
C c;
b.a();
c.a();
cout << endl;
A* array[5];
array[0] = &b.a; //different functions with their ressources
array[1] = &c.a;
array[2] = &b.a;
array[3] = &c.a;
array[4] = &c.a;
for (int i = 0; i < 5; i++) //this usability i wanted to provide
{
(*(array[i]))();
}
getchar();
return 0;
}
Output :
FooBar
FooBarFooBarBar
This is as far as i can press it down concerning examples. But i guess this is unsafe code. I stumbled across possible other and simpler ways to achieve this (other uses of std::function and lambdas(which i might have tried to reinvent here partially it seems)).
At first I had tried to pass "this" to the bind function in function<void()>*AFunctionConstructor::constructFunction(CallerTypeFunc func)
,though, which i now get through the dynamic upcast.
Additionally the functionality of AFunctionConstructor was first supposed to be implemented in a Constructor of A.
how to properly address this?
class House{
public:
void startAction();
void init(){
startAction = [] () {};
}
};
I have tried this, but it is said "Expression is not assignable"
I want to define the function of startAction, but inside the init method.
I do this because there's couple of variable in init method that i want to capture to pass to startAction.
You can't assign a value to a member function.
You could have a member variable that the member function calls, something like this:
class House{
public:
void startAction() { m_action(); }
void init(){
m_action = [] () {};
}
private:
std::function<void()> m_action;
};
But it's simpler to store the things you want to capture as members of House.
Try this:
class House {
public:
std::function<void ()> startAction;
void init() {
startAction = []() {};
}
};
int main() {
House house;
house.init();
house.startAction();
...
}
You can also instead of using std::function<void ()> use C style pointer to function. Look at the following code.
#include <iostream>
typedef void (*my_func)();
class House
{
public:
my_func m_ffunc;
void init()
{
m_ffunc = [](){ std::cout << "Hello from m_ffunc" << std::endl;};
}
};
int main()
{
House myHouse;
myHouse.init();
myHouse.m_ffunc();
return 0;
}