How can I override a virtual member function of the following type:
virtual AnimalId func(int index) const
where AnimalId is a typedef unsigned int
I tried several ways but either ending up by an error that I don't give output or that I don't have an overrider at all. I saw on some website that maybe I need to use static const in order to do this, but I don't know how.
In order to override method of signature
virtual AnimalId func(int index) const
declared in base class Base, you have to define function with same signature in derived class:
class Derived : public Base {
public:
virtual AnimalId func(int index) const
{
return 43; // I am using 43 because I think this is
// so much underestimated in favor of 42
}
//...
};
Or you can type override kryword to be more explicit:
class Derived : public Base {
public:
virtual AnimalId func(int index) const override
{
return 43 & 45;
}
//...
};
Did you mean something like: (Note, you have to remove override if you compile for C++03)
typedef unsigned int AnimalId;
class Base
{
public:
virtual ~Base() {}
virtual AnimalId func(int index) const { return 0; }
};
class Derived : public Base
{
public:
AnimalId func(int index) const override { return 42; }
};
Related
I have two interfaces:
class IElement
{
public:
virtual ~IElement() {}
};
class IContainer
{
public:
virtual ~IContainer() {}
virtual const std::vector<IElement *> &elements() const = 0;
};
The member elements of IContainer is supposed to return a vector of pointers to IElement.
Then I have two concrete implementations:
class CConcreteElement: public IElement
{
public:
virtual ~CConcreteElement() {}
void doSomething() { /* ... */ }
};
class CConcreteContainer: public IContainer
{
private:
std::vector<CConcreteElement *> m_vecElements;
public:
virtual ~CConcreteContainer() {}
virtual const std::vector<IElement *> &elements() const override
{ return m_vecElements; } // PROBLEM HERE
void doSomething()
{
for (CConcreteElement *pE : m_vecElements)
{
pE->doSomething();
}
}
};
The above does not compile, because m_vecElements has the type const std::vector<CConcreteElement *> while the return value is supposed to have the type const std::vector<IElement *>. Apparently, C++ does not realize the types IElement and CConcreteElement are related when they appear as a template argument.
I also tried:
return static_cast<const std::vector<IElement *>>(m_vecElements);
But this doesn't compile either. Any ideas how I could get this to work?
You cannot perform this cast because std::vector is invariant on its element type which means that there's no relation (in the inheritance sense) between different vectors even if their elements are related. For more information about type variance please see this article.
The short answer is that you cannot do this exact thing with std::vector but you can do this by providing an element getter instead of all elements getter:
class IContainer
{
public:
virtual ~IContainer() {}
virtual const IElement *element(size_t index) const = 0;
};
class CConcreteContainer: public IContainer
{
private:
std::vector<CConcreteElement *> m_vecElements;
public:
virtual ~CConcreteContainer() {}
virtual const CConcreteElement *element(size_t index) const override
{
return m_vecElements[index];
}
};
Note that return type of the overriding method is different from the base method. This is possible because C++ supports covariant return types.
Consider the following code:
#include <stdio.h>
#include <iostream>
/// Header-file
class Base {
public:
virtual void do_something() const =0;
int GetAttrib () const {return constattribute_;};
static const int constattribute_;
};
typedef Base* Derived_Ptr; //<< adress derived classes by their base-class ptr; so no templates for Base
class DerivedA : public Base {
// static const int constattribute_; //<< change this static attribute for all DerivedA class instances and their derivatives
void do_something() const {};
};
class DerivedB : public Base {
// static const int constattribute_; //<< change this static attribute for all DerivedB class instances and their derivatives
void do_something() const {};
};
/// CC-file
using namespace std;
const int Base::constattribute_(0);
const int DerivedA::constattribute_(1); //<<error: no such variable 'constattribute' in class DerivedA
const int DerivedB::constattribute_(2); //<<error: no such variable 'constattribute' in class DerivedB
int main(void) {
Derived_Ptr derivedA = new DerivedA();
Derived_Ptr derivedB = new DerivedB();
cout << derivedA->GetAttrib() << derivedB->GetAttrib() <<endl;
return 0;
};
The intend being that i have some abstract interface (Base) which defines also a variable, which should be present for all derived classes, and is retrievable. All flavours of subclasses should be forced to/able to redefine their specific value for this variable, at best during class declaration (the values are known at the time the class is declared after all).
I want to achieve code, not altering the main()-program so that the output is '12' and not as of now (uncommenting current lines in the code) '00' (Doing so shadows the fields from base class).
I tried to look into the matter, and there are different paths for solutions, many of which however go contrary to my intuition:
1. Some follow the CRTP pattern, which is however impossible if I want to address my subclasses by their base-ptr in main.
2. Other solutions require to virtualize the 'GetAttrib()' function for every derived instance., which is cumbersome, and action of modifying the attribute is masked within a function definition.
3. A third possibility is to remove the static pattern and have the 'constattribute_' field as a regular member, which however forces me to drag it through all constructors as a parameter.
I am quite sure that there must be some smarter way to do this. Any hints are appreciated.
Using CRTP may get you what you want, assuming you don't have to access GetAttr() through Base* and can leave without constattribute_ in Base itself. Just follow the rule that every programming problem can be solved by entering another level of indirection, which I did below:
class Base {
public:
virtual void do_something() const = 0;
virtual ~Base() // should define it as you are using Base*
{
}
};
typedef Base* Derived_Ptr;
template<class T>
class BaseConstAttr : public Base
{
public:
int GetAttrib () const
{
return(constattribute_);
};
static const int constattribute_;
};
class DerivedA : public BaseConstAttr<DerivedA>
{
public:
void do_something() const
{
};
};
class DerivedB : public BaseConstAttr<DerivedB>
{
public:
void do_something() const
{
};
};
template<> const int BaseConstAttr<DerivedA>::constattribute_(1);
template<> const int BaseConstAttr<DerivedB>::constattribute_(2);
If you need GettAttr from top to bottom of the inheritance tree you can modify the above code a bit, but this will cost you making GetAttr virtual (but still one implementation only):
class Base {
public:
virtual void do_something() const = 0;
virtual int GetAttrib () const = 0;
virtual ~Base() // should define it as you are using Base*
{
}
};
typedef Base* Derived_Ptr;
template<class T>
class BaseConstAttr : public Base
{
public:
int GetAttrib () const
{
return(constattribute_);
};
static const int constattribute_;
};
class DerivedA : public BaseConstAttr<DerivedA>
{
public:
void do_something() const
{
};
};
class DerivedB : public BaseConstAttr<DerivedB>
{
public:
void do_something() const
{
};
};
template<> const int BaseConstAttr<DerivedA>::constattribute_(1);
template<> const int BaseConstAttr<DerivedB>::constattribute_(2);
Please note that I don't know how well (or bad) it will behave with deep inheritance tree (ie. when inheriting from DerivedA and/or DerivedB). In this case I would probably remove BaseConstAttr from inheritance tree right below Base and would try to inject it between most derived class and its predecessor or use multiple inheritance.
What you are requesting requires virtual dispatch somewhere, because you don't know the type of the object you are dealing with until runtime. The purpose of virtual dispatch is to solve exactly the problem you are facing.
The simplest solution is what you have given as number 2: make GetAttrib() virtual, and implement it on each derived class where you introduce a shadowing constattribute_.
static variable in base class is single instance hence it will be reflected same in derived class.
You can make same static member variable in derived class with specific different value you want. Now make getter member function of static variable in Base class as virtual and overload it in derived class which returns is static instance value.
I have update your code to work it, please check ..
#include <iostream>
using namespace std;
class Base {
public:
static const int constattribute_;
virtual void do_something() const =0;
virtual int GetAttrib () const {return constattribute_;};
};
typedef Base* Derived_Ptr; //<< adress derived classes by their base-class ptr; so no templates for Base
class DerivedA : public Base {
static const int constattribute_; //<< change this static attribute for all DerivedA class instances and their derivatives
void do_something() const {};
int GetAttrib () const {return constattribute_;};
};
class DerivedB : public Base {
static const int constattribute_; //<< change this static attribute for all DerivedB class instances and their derivatives
void do_something() const {};
int GetAttrib () const {return constattribute_;};
};
const int Base::constattribute_(0);
const int DerivedA::constattribute_(1); //<<error: no such variable 'constattribute' in class DerivedA
const int DerivedB::constattribute_(2); //<<error: no such variable 'constattribute' in class DerivedB
int main(void) {
Derived_Ptr derivedA = new DerivedA();
Derived_Ptr derivedB = new DerivedB();
cout << derivedA->GetAttrib() << derivedB->GetAttrib() <<endl;
return 0;
};
You should get desired output.
Note : Remember all member variables and func in derived class are private.
The following code
#include <iostream>
template<typename T>
class Abstract
{
public:
virtual ~Abstract()
{}
virtual void func(const T ¶m) const = 0;
};
class Concrete : public Abstract<int*>
{
public:
virtual void func(const int *¶m) const override // watch out!
{}
};
int main()
{
Concrete c;
}
produces the following compile error at the line marked with the comment:
error C3668: 'Concrete::func' : method with override specifier 'override' did not override any base class methods
If I remove the override, this error will be shown:
error C2259: 'Concrete' : cannot instantiate abstract class
How to derive from Abstract<int*>?
Declare Concrete like this:
class Concrete : public Abstract<int*>
{
public:
virtual void func(int * const ¶m) const override
{}
};
The question is: Which part of the parameter of Abstract<int*>::f() is const?
The answer: Since T is const and T in Abstract<int*> is a pointer (to an int), it is the pointer that is const - not the int.
You want this:
virtual void func(int * const ¶m) const override // watch out!
I have a base class
class Shape{
public:
virtual int getArea()=0;
}
I want to change the signature while overriding like this:
class Rectangle : class Shape{
public:
int getArea(int someParameter = 0){
return 0;
}
}
Is it possible somehow to achieve this, as I am using default value for newly added parameter?
The only solution I can see is that you implement both int getArea() and int getArea(int), where one function can call the other (for example int getArea() { return getArea(0); }). You can not have the overload taking an argument have a default argument then.
You need to overload your virtual function. But if you overloaded virtual function, you will get a warning "hides overloaded virtual function"
Solution is override function with native signature and overload it with virtual prefix.
Example:
class A {
virtual void foo(int) {...}
};
class B: public A {
void foo(int i) override {
A::foo(i);
}
virtual void foo(const std::string& str) {
...
}
};
I have implemented the following interface:
template <typename T>
class Variable
{
public:
Variable (T v) : m_value (v) {}
virtual void Callback () = 0;
private:
T m_value;
};
A proper derived class would be defined like this:
class Derived : public Variable<int>
{
public:
Derived (int v) : Variable<int> (v) {}
void Callback () {}
};
However, I would like to derive classes where Callback accepts different parameters (eg: void Callback (int a, int b)).
Is there a way to do it?
This is a problem I ran in a number of times.
This is impossible, and for good reasons, but there are ways to achieve essentially the same thing. Personally, I now use:
struct Base
{
virtual void execute() = 0;
virtual ~Base {}
};
class Derived: public Base
{
public:
Derived(int a, int b): mA(a), mB(b), mR(0) {}
int getResult() const { return mR; }
virtual void execute() { mR = mA + mB; }
private:
int mA, mB, mR;
};
In action:
int main(int argc, char* argv[])
{
std::unique_ptr<Base> derived(new Derived(1,2));
derived->execute();
return 0;
} // main
Even if such a thing were possible, it no longer makes much sense to have it as a virtual function, as the derived instantiations couldn't be called polymorphically via a pointer to the base class.
don't think this will be possible, because you can never interface it back to Variable.
This is what i mean
int a=0; int b = 0;
Variable<int>* derived = new Derived();
derived->Callback(a, b); //this won't compile because Variable<int> does not have Callback with 2 vars.
I know this there is an accepted answer, but there is one (ugly) way to achieve what you want, although I would not recommend it:
template <typename T>
class Variable
{
public:
Variable (T v) : m_value (v) {}
virtual void Callback (const char *values, ...) = 0;
private:
T m_value;
};
class Derived : public Variable<int>
{
public:
Derived (int v) : Variable<int> (v) {}
virtual void Callback (const char *values, ...) {
}
};
Now, you can use:
int a=0;
double b = 0;
Variable<int>* derived = new Derived(3);
derived->Callback("");
derived->Callback("df", a, b);
You need the values argument in order to obtain the remaining arguments inside the method. You also need to know the argument types, and pass them like printf does.
This method is error prone, as you must match the argument types on values with the real argument types.
You will have to add an overload of Callback in the base class that accepts these parameters. It would also be possible to do bad things, like accept a void*, or pass in a raw pointer-to-bytes.
The only scenario in which it is valid to alter virtual function signature is when you override the return value to something polymorphic to the original return value, e.g. *this.