I am attempting to use functors in a larger-scope project. I managed to implement a functor when it was on its own, but I am having a lot of trouble figuring out where I define things in the header file (.h) vs the .ccp file so that I can access my functor in the rest of my program. When the functor is all together it appears as such:
class SpecialAttack {
public:
SpecialAttack() {};
virtual bool operator() (double timer) = 0;
};
class BallAttack : public SpecialAttack {
public:
BallAttack() {};
virtual bool operator() (double timer) { return (timer==0); }
};
class SpiderAttack : public SpecialAttack {
public:
SpiderAttack() {};
virtual bool operator() (double timer) { return true; }
};
double special_attack(double timer, SpecialAttack* func) {
return (*func)(timer);
}
I cannot figure out how to break this up into the appropriate files so that I can then reference it in the rest of my code. What parts should i put in the .h file and what parts go in the .ccp file? Thanks!
Splitting functors between .hpp and .cpp files follows the same pattern of declaration and definition used for any class with member functions.
attack.hpp
#pragma once
class SpecialAttack {
public:
SpecialAttack();
virtual bool operator() (double timer) = 0;
};
class BallAttack : public SpecialAttack {
public:
BallAttack();
virtual bool operator() (double timer) override;
};
class SpiderAttack : public SpecialAttack {
public:
SpiderAttack();
virtual bool operator() (double timer) override;
};
attack.cpp
#include "attack.hpp"
SpecialAttack::SpecialAttack() {
}
BallAttack::BallAttack() {
}
/*virtual*/ bool BallAttack::operator() (double timer) /*override*/ {
return (timer==0);
}
SpiderAttack::SpiderAttack() {
}
/*virtual*/ bool SpiderAttack::operator() (double timer) /*override*/ {
return true;
}
Related
I have a class MyClass (with several virtual functions) that performs operations on an object called MyType.
The class MyClassImpl inherits MyClass and implements the virtual functions, but I need to add additional members to MyType, but I don't want to modify the class MyType (instead I want to keep it generic).
Now, if I make a MyTypeImpl and inherit MyType, I can add members. But, how do I make the non virtual functions in MyClassImpl (inherited from MyClass) use the new MyTypeImpl?
The only way I can think is to make MyClass use MyTypeImpl but I want to avoid using the implementation in the generic class because I might use various different implementations.
Here is a simple example of what the classes might look like. Of course, the code will not compile because the methods and members added in MyTypeImpl and not MyType.
class MyType {
public:
void increment() {
data_++;
}
protected:
int data_ = 0;
};
class MyClass {
public:
void alg() {
sub_routine_1();
sub_routine_2();
modify_mytype();
};
protected:
MyType mytype_;
virtual void sub_routine_1() = 0;
virtual void sub_routine_2() = 0;
void modify_mytype() {
mytype_.increment();
};
};
class MyTypeImpl : public MyType {
public:
void decrement() {
data_--;
is_decremented = true;
};
protected:
bool is_decremented = false;;
};
class MyClassImpl : public MyClass{
public:
void print() {
mytype_.print();
};
protected:
virtual void sub_routine_1() {
//do algorithm things here
mytype_.increment();
mytype_.increment();
};
virtual void sub_routine_2() {
//do more algorithm things here
mytype_.decrement();
mytype_.decrement();
};
};
After seeing your example I see now that you just want to extend the functionality of that class without modifying the original class. If you need to add additional functions, but you don't want to change the type that is stored in MyClass there isn't any way I know of to make that happen without at least modifying MyType to include virtual functions for the functions you want to call.
You also need to make MyClass take a pointer to MyType so you can use polymorphism and make the calls resolve to the correct implementation:
Dynamic Polymorphism Solution:
#include <iostream>
class MyType {
public:
virtual void increment() {
data_++;
}
// To be implemented by implementation class
virtual void print() = 0;
// To be implemented by implementation class
virtual void decrement() = 0;
protected:
int data_ = 0;
};
class MyTypeImpl : public MyType
{
public:
void print() {
std::cout << 42 << std::endl;
}
void decrement() {
data_--;
is_decremented = true;
};
protected:
bool is_decremented = false;;
};
class MyClass {
public:
MyClass(MyType* mytype)
: mytype_(mytype)
{}
void alg() {
sub_routine_1();
sub_routine_2();
modify_mytype();
};
protected:
MyType* mytype_;
virtual void sub_routine_1() = 0;
virtual void sub_routine_2() = 0;
void modify_mytype() {
mytype_->increment();
};
};
class MyClassImpl : public MyClass{
public:
MyClassImpl(MyType* mytype)
: MyClass(mytype)
{}
void print() {
mytype_->print();
};
protected:
virtual void sub_routine_1() {
//do algorithm things here
mytype_->increment();
mytype_->increment();
};
virtual void sub_routine_2() {
//do more algorithm things here
mytype_->decrement();
mytype_->decrement();
};
};
int main()
{
MyType* mytype = new MyTypeImpl();
MyClass* myclass = new MyClassImpl(mytype);
// Prints "42"
myclass->print();
// Do other stuff with "myclass"
delete myclass;
delete mytype;
}
Note, I am only using a raw pointer in this example for increased clarity. It is highly recommended that you don't use new and delete and use smart pointers to manage the lifetime of your pointers instead.
Static Polymorphism Solution:
Not that the design of this solution is actually any better, but I think this is closer to what you are actually looking for because it doesn't require modifying the MyType class directly. Also the only modification needed for MyClass is to make it a template class:
#include <iostream>
class MyType {
public:
virtual void increment() {
data_++;
}
protected:
int data_ = 0;
};
class MyTypeImpl : public MyType
{
public:
void print() {
std::cout << data_ << std::endl;
}
void decrement() {
data_--;
is_decremented = true;
};
protected:
bool is_decremented = false;
};
template <typename T>
class MyClass {
public:
void alg() {
sub_routine_1();
sub_routine_2();
modify_mytype();
};
protected:
T mytype_;
virtual void sub_routine_1() = 0;
virtual void sub_routine_2() = 0;
void modify_mytype() {
mytype_.increment();
};
};
template <typename T>
class MyClassImpl : public MyClass<T> {
public:
void print() {
this->mytype_.print();
};
protected:
virtual void sub_routine_1() {
//do algorithm things here
this->mytype_.increment();
this->mytype_.increment();
};
virtual void sub_routine_2() {
//do more algorithm things here
this->mytype_.decrement();
this->mytype_.decrement();
};
};
int main()
{
// Use the template to get the correct implementation
MyClassImpl<MyTypeImpl> myclass;
myclass.alg();
myclass.print();
// Do other stuff with my class
}
I have an interface and a class implementing it:
class InterfaceA
{
public:
enum Enum { X, Y, Z };
virtual void foo(Enum &) = 0;
virtual void bar() = 0;
};
class ClassA : public InterfaceA
{
public:
virtual void foo(Enum & a) {
a = X;
}
virtual void bar();
};
I need to extend the functionality of an InterfaceA implementing class in one aspect. In my understanding this is what mixins are for.
E.g. foo() should change its parameter value on some condition. Here is how I do it:
template <typename T>
class Mixin : public T
{
public:
virtual void foo(Enum & a) {
T::foo(a);
if (a == X){
a = Y;
}
}
};
The problem is, it won't compile unless I either write scope prefixes to identifiers defined in the interface class or typedef them like this:
template <typename T>
class Mixin : public T
{
public:
typedef InterfaceA::Enum Enum;
virtual void foo(Enum & a) {
T::foo(a);
if (a == InterfaceA::X){
a = InterfaceA::Y;
}
}
};
It's not a big deal if there are few such types coming from the interface definition. But if there are many, it can become ugly. I would like the code to reflect the fact that a Mixin manipulates an InterfaceA object by design. Unfortunately inheriting Mixin from InterfaceA introduces a 'dreaded diamond' which I'd better avoid. Is that possible?
Enum, X and Y should be dependent typename.
And you may use static_assert to force inheritance.
template <typename T>
class Mixin : public T
{
public:
static_assert(std::is_base_of<InterfaceA, T>::value,
"T should inherit from InterfaceA");
virtual void foo(typename T::Enum & a) {
T::foo(a);
if (a == T::X){
a = T::Y;
}
}
};
I suppose you should use virtual inheritance; this should avoid "dreaded diamond" problem.
Something like
class InterfaceA
{
public:
enum Enum { X, Y, Z };
virtual void foo(Enum &) = 0;
virtual void bar() = 0;
};
class ClassA : public virtual InterfaceA
{
public:
virtual void foo(Enum & a) {
a = X;
}
virtual void bar() {
}
};
template <typename T>
class Mixin : public T, public virtual InterfaceA
{
public:
virtual void foo(Enum & a) {
T::foo(a);
if (a == X){
a = Y;
}
}
};
int main ()
{
Mixin<ClassA> m;
return 0;
}
I'm trying to implement the double dispatch pattern but I get a circular dependency that I can't resolve with forward declaration (as it was resolved in this problem link).
Below is an example of my problem:
header 1:
class Object
{
virtual void dispatchAdd(Collection& c) const = 0;
};
class Blockage: Object
{
virtual void dispatchAdd(Collection& c) const
{
c.add(*this);
}
};
class Boundary: Object
{
virtual void dispatchAdd(Collection& c) const
{
c.add(*this);
}
};
header 2:
class Collection
{
public:
void add(const Blockage& b)
{ ... }
void add(const Boundary& b)
{ ... }
...
private:
boost::unordered_set<Boundary> m_boundaries;
boost::unordered_set<Blockage> m_blockages;
}
I can't forward declare Boundary and Blockage in header 2, because I need a complete type to use boost::unordered_set. Any advice to resolve this problem?
Forward declare Collection in header 1 and move the implementations for dispatchAdd out of the header file and into the source file.
objects.h (i.e. "header 1"):
class Collection;
class Object
{
virtual void dispatchAdd(Collection& c) const = 0;
};
class Blockage: Object
{
virtual void dispatchAdd(Collection& c) const;
};
class Boundary: Object
{
virtual void dispatchAdd(Collection& c) const;
};
objects.cpp
#include "objects.h" // i.e. "header 1"
#include "collection.h" // i.e. "header 2"
void Blockage::dispatchAdd(Collection& c) const
{
c.add(*this);
}
void Boundary::dispatchAdd(Collection& c) const
{
c.add(*this);
}
I've implemented my callbacks with interface..
struct ICallback
{
virtual bool operator()() const = 0;
};
and function for adding a callback
void addCallback(const ICallback* callback) { .... }
and use, callback is in some class
class BusImplemantation{
public:
struct Foo : ICallback
{
virtual bool operator()() const { return true;}
}foo;
void OtherMethod();
int OtherMember;
};
But because callback is class(not function/method), I cant within callback access to OtherMethod and OtherMember. If callback would be not class, but only method than it would be possible.(inner class vs. method)
I cant pass OtherMethod and OtherMember to callback as parameters.
Is there any better solution for that? maybe with templates?
Use std::function:
void addCallback(const std::function<bool()>) { .... }
class BusImplemantation{
public:
bool Callback() { return true; }
void OtherMethod();
int OtherMember;
};
BusImplemantation obj;
addCallback(std::bind(&BusImplemantation::Callback, obj));
Check out boost::bind for a bunch of alternatives on how to implement this.
Could you do something like this instead:
typedef std::function<bool()> CallbackFunc;
void addCallback(const CallbackFunc callback) { .... }
class BusImplemantation{
public:
struct Foo
{
Foo(member) : OtherMember(member) { }
bool operator()() const { return true; }
void OtherMethod();
int OtherMember;
}foo;
};
Instead of making your callback an interface, make it use std::function to make it a function object (a Functor), and any extra data or methods that your functor needs can be a part of the functor class.
The whole point of using callback objects instead of free functions is that you can associate arbitrary state with them:
class BusImplemantation{
public:
struct Foo : ICallback
{
explicit Foo(BusImplementation &p) : parent(p) {}
virtual bool operator()() const;
private:
BusImplementation &parent;
} foo;
BusImplementation() : foo(*this)
{
addCallback(&foo)
}
void OtherMethod();
int OtherMember;
};
bool BusImplementation::Foo::operator() () const
{
if (parent.OtherMember == 0) {
parent.OtherMethod();
return false;
}
return true;
}
I think your ICallback interface must have pointer to controlled class with base interface, assume it BusImplemantation and inside callback use this pointer.
struct ICallback
{
virtual bool operator()() const = 0;
void setControlObject(BusImplemantation* o)
{
obj = o;
}
BusImplemantation* obj;
};
class BusImplemantation
{
public:
void addCallback(const ICallback* callback)
{
callback->setControlObject(this);
}
void OtherMethod();
int OtherMember;
};
And use:
class SomeCb : ICallback
{
bool operator()
{
obj->OtherMethod();
return true;
}
}
Here is the code I am currently troubleshooting:
void CTimer::notify()
{
std::vector<IObserver*>::iterator it;
for(it=observers.begin();it!=observers.end();++it)
{
ITimerNotification* notification = new CTimerNotification(now());
(*it)->readNotification(*notification);
}
}
class CTimerNotification : public ITimerNotification
{
public:
CTimerNotification(const timeval& t)
{
time = t;
}
protected:
timeval time;
private:
virtual ~CTimerNotification();
virtual void read(const IObserver& o) const
{
o.update(*this);
}
virtual const timeval& getTime() const
{
return time;
}
};
class IObserver
{
public:
virtual ~IObserver();
virtual void readNotification(const INotification&) const=0;
virtual void update(const INotification&) const=0;
};
class ITimerObserver : public IObserver
{
public:
virtual void update(const ITimerNotification&) const=0;
};
class TestObserver : public ITimerObserver
{
public:
virtual void readNotification(const INotification& n) const
{
n.read(*this);
}
virtual void update(const INotification& n) const
{
std::cout<<"???: TestObserver: update()!\n";
}
virtual void update(const ITimerNotification& n) const
{
std::cout<< n.getTime().tv_sec << "." << n.getTime().tv_usec <<": TestObserver: update()!\n";
}
};
So the code runs, CTimer::notify() gets called, which creates a TimerNotification and passes it to the observer via readNotification() which in turn calls the notification's read() method, which finally calls the observer's (hopefully) correct update() method.
The last step is what fails. It calls the update(INotification&) method instead of the desired update(ITimerNotification&) method.
What am I missing here for this attempted Double Dispatch pattern to work? It does not appear to be getting the correct type information to select the appropriate function call.
Thanks for any help!
CTimerNotification needs a read something like this
virtual void read(const IObserver& o) const {
ITimerObserver* to = dynamic_cast<ITimerObserver*>(&o);
if (to) {
to->update(*this);
} else {
o.update(*this);
}
}
and you need a using IObserver::update; in ITimerObserver.