About delegates in c++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I specify the parameter of a method as any class that implements a specific interface ?
This is rather common in objective c.

There are no interfaces in standard C++, but we can simulate them pretty easily:
class IComparable
{
protected:
IComparable() {};
public:
virtual ~IComparable() = 0 {};
virtual int Compare(const IComparable& other) const = 0;
};
There is no way we can instantiate this class. It is effectively an interface. You can then derive concrete classes from this.

If you have an "interface" or abstract base class called Base, then a function which can accept any object implementing that interface would look like:
void fn(Base& obj) {
/*use Base functions on obj...*/
}

Related

How to call base class virtual function in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Suppose I have two classes class A and class B, class B is derived from class A public. Here class A have virtual emp(),and class B have emp(),
In this case how can I call base class virtual function?
You can invoke A::emp() directly
B* obj = new B();
b->A::emp();
Or within a method of A or B.
void B::SomeOtherMethod()
{
A::emp(); // same as this->A::emp();
}

C++ inheritance for classes that depend on one another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to design an abstract class in C++. It is called AbstractCurve and it has
AbstractPoint's on it that are made up of x and y coordinates which are AbstractFieldElements.
I want to inherit from this to make, for example, a PrimeCurve that has PrimePoints and
PrimeFieldElements.
If I do something like this:
class AbstractPoint {
private:
AbstractFieldElement* x_;
AbstractFieldElement* y_;
};
How will I tell the compiler to make x_ and y_ be PrimeFieldElements when I create
the PrimePoint class?
Also, is there some better way to do this that I am missing?
Assuming the only difference between the classes is these datatypes, I would do something like this:
template <typename ElementType>
class Point {
private:
ElementType* x_;
ElementType* y_;
};
using AbstractPoint = Point<AbstractFieldElement>;
using PrimePoint = Point<PrimeFieldElement>;
If we take this to the next level up, we can keep going
template <typename ElementType>
class Curve {
private:
using Point = Point<ElementType>;
std::vector<Point> points_;
};
using AbstractCurve = Curve<AbstractFieldElement>;
using PrimeFieldCure = Curve<PrimeFieldElement>;
It is reasonable to use inheritance as you are suggesting to provide common behavior to a variety of curve/point types, but if the different types will use different internal data types, then templates are the way to go.

Using generic template member in a class C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating class that has a templated object (Item<T>) as a member, basically like this:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<T>> vec;
};
Currently, I have Item<string>, but I need to be able to use it with non string objects. Is there a way to do this without templating myClass (which would obviously be a lot of work for a complicated class)?
If your class will only use Item< string>, you may try:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<string>> vec;
};
But if you want any other type of Item in the vector, the answer is No, there is no magic solutions.

How to call a function in all inheritant classes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For example let's say I have this class:
class base{
public:
update();
};
and I have a lot of classes that are inherit from this one:
class A : public base
{
//<...>
}
class B : public base
{
//<...>
}
and so i want to know - can i somehow call the function base::update(); in all the classes without writing it all out (A::update(); B::update();) etc.
Like calling one function that would do the update(); in all the classes that inherit it from the base class.
Thanks!
Edit:
What I'm doing is changing my entity system into component based one and I want to know whether there's an easier way to call the (let's say) update(); function, that is inherited, in all the members. Instead of doing enemy01.update(); enemy02.update(); etc. to just write down a single update(); that'd work on all classes that have it(inherited it), sorta like a message to all of them to call the function.
A virtual function?
class base{
public:
virtual void update();
};
And then this function:
void update_class(base &b) {
return b.update();
}
You can call update_class on any class that inherits base.

C++ non-type template template parameters, reference to *this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to improve my generic architecture, and I've decided that a system that uses this type of ambiguous context referencing would be ideal for my purposes. However, I'm having trouble figuring out how to get the syntax to work. I don't even know if something like this is possible! I have slightly shiestier alternatives that accomplish mostly the same thing but this would be best:
class IContained
{
public:
virtual int getInt() = 0;
};
typedef std::shared_ptr<IContained>IContainedPtr;
template<template<class RefType, RefType& itsRef> class ContainedType>
class TestClass
{
TestClass() :
myContained(new ContainedType < TestClass, *this>())
{
}
int getContextInt()
{
return 3;
}
IContainedPtr myContained;
};
template<class RefType, RefType& itsRef>
class Contained:
virtual public IContained
{
int getInt()
{
return itsRef.getContextInt();
}
};
TEST(POTATO, PARTY)
{
TestClass<Contained> myTest();
int thing = myTest.myContained->getInt();
EXPECT_EQ(thing, 3);
}
I am not sure what do you want to implement but I can explain why you are not able to use
*this
as template argument. Templates provide you with compile-time polymorphism. It means that all templates arguments should be known at compile time.
this
is a class instance variable, is an address of class instance, so it could not be determined during compilation. The same as
*this