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();
}
Related
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 6 years ago.
Improve this question
code is
struct {
protected:
static int labelCounter;
};
protected members can be assigned in methods that belong to the class, or any derived class.
Your static member can be initialized normally, but you have to give a name to the struct:
struct MyStruct { protected: static int labelCounter; };
// .cpp:
int MyStruct::labelCounter = 12;
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.
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
In a method of an object of type class A, I handle an object of class B that has a public method .join(*A).
I want my object of type A calling this someObjectOfTypeB.join(*A) method, to use a pointer to itself as the parameter.
void A::someMethod()
{
B b();
b.join(I want to a to use a pointer to itself as a parameter);
}
A a();
a.someMethod();
Upon further investigation, this was not the problem as I led myself to believe; and is indeed the correct way of doing what I wanted to do.
Try using this:
void A::someMethod()
{
B b;
b.join(this);
}
As #AndrewLazarus and #JonathanWakely commented, use B b; instead of B b(). The later declares a function b without parameters which returns B, and that is not what you want.
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.
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...*/
}