Is there a good practice to access a derived virtual function from it's base class?
Here's what I got so far:
class A {
public:
enum class AType{A,B,C,D};
AType aType = AType::A;
virtual void Do(){}
};
class B : public A {
public:
int val = 0;
B(int i) {
aType = AType::B;
val = i;
}
void Do(){
std::cout << val << std::endl;
}
};
int main(){
std::map<std::string, A> bunch;
bunch["01"] = B(10);
bunch["02"] = B(15);
bunch["03"] = B(80);
for (auto& [k, b] : bunch){
switch (b.aType){
case A::AType::A:
break;
case A::AType::B:
static_cast<B&>(b).Do();
break;
}
}
}
I'm not too happy about using a switch there, any advice would be appreciated!
You should assign a pointer or reference to use polymorphism in C++. If you assign a child class as value, there would be a object slicing. I'd use std::unique_ptr than raw pointer like following, which prints out 10 15 80 as excepted.
#include <memory>
#include <map>
class A {
public:
enum class AType { A, B, C, D };
AType aType = AType::A;
virtual void Do() {}
};
class B : public A {
public:
int val = 0;
B(int i) {
aType = AType::B;
val = i;
}
void Do() override {
std::cout << val << std::endl;
}
};
int main() {
std::map<std::string, std::unique_ptr<A>> bunch;
bunch["01"] = std::make_unique<B>(10);
bunch["02"] = std::make_unique<B>(15);
bunch["03"] = std::make_unique<B>(80);
for (auto& [k, b] : bunch) {
b->Do();
}
return 0;
}
First, what're showing isn't what I'd call "calling from base class". Otherwise,
calling derived virtual methods from a base class method is one of the main reasons to use virtual functions. The other reason is to call a method via pointer to base class, like you intend to do.
Also, as mentioned above, in:
bunch["01"] = B(10);
a temporary object of B type is silently cast (sliced) to object of class A. You should use perhaps map of pointers to A.
Related
I want to make something functionally similar to this:
class Base
{
public:
const int ClassID = 1;
}
class Derived1 : public Base
{
public:
const int ClassID = 2;
}
class Derived2 : public Base
{
public:
const int ClassID = 3;
}
But, obviously, you can't override variables. What would be the best way to achieve the same functionality?
(The context for this is a video game where there are different troops, overridden from the same parent "BaseTroop" class. I want each troop to have its own ID that can be retrieved from anywhere)
Thanks in advance!
There is no way to change the default member initialiser in derived classes.
But, there is no need to rely on the default initialiser. You can provide an initialiser in the constructor:
struct Derived1 : Base
{
Derived1(): Base{2} {}
};
struct Derived2 : Base
{
Derived2(): Base{3} {}
};
Add a virtual function, returning the ID. You can also have a static variable or function returning the same ID, in case you want to get it without a class instance. You can also assign the IDs automatically, using CRTP:
#include <iostream>
struct BaseLow
{
virtual int GetId() const = 0;
virtual ~BaseLow() {}
};
namespace impl
{
int &GetIdCounter()
{
static int ret = 0;
return ret;
}
}
template <typename Derived>
struct Base : BaseLow
{
inline static const int id = impl::GetIdCounter()++;
int GetId() const override final
{
return id;
}
};
struct Derived1 : Base<Derived1> {};
struct Derived2 : Base<Derived2> {};
int main()
{
std::cout << Derived1::id << '\n'; // 0
std::cout << Derived2::id << '\n'; // 1
Derived1 d1;
Derived2 d2;
BaseLow *ptrs[] = {&d1, &d2};
for (BaseLow *ptr : ptrs)
std::cout << ptr->GetId() << '\n'; // 0, 1
}
In the virtual method create() in the derived class Derived, I return a struct of type HelpDerived. However, since I had to set the return type of the method to HelpBase, I found that I need to cast the returned object back to the type HelpDerived.
The following is an example of my case.
#include <iostream>
struct HelpBase {
int a = 0;
virtual void output() {}
};
struct HelpDerived : HelpBase {
int b = 0;
void output() override {}
};
class Base {
public:
virtual HelpBase create() = 0;
};
class Derived : public Base {
public:
HelpBase create() override;
};
HelpBase Derived::create() {
HelpDerived d;
d.a = 1;
d.b = 2;
return d;
}
int main() {
Derived d;
auto based = d.create();
HelpDerived derived = dynamic_cast<HelpDerived &>(based);
std::cout << derived.a << std::endl;
}
When I run the code abve, I get the error
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap: 6
What have I misunderstood about objects and casting in C++? Why does this method not work?
What can I do to fix the problem?
I think you'd better return a pointer to avoid object slicing in your create function.
#include <iostream>
struct HelpBase {
int a = 0;
virtual void output() {}
};
struct HelpDerived : HelpBase {
int b = 0;
void output() override {}
};
class Base {
public:
virtual HelpBase* create() = 0;
};
class Derived : public Base {
public:
HelpBase* create() override;
};
HelpBase* Derived::create() {
HelpDerived* d = new HelpDerived;
d->a = 1;
d->b = 2;
return d;
}
int main() {
Derived d;
auto based = d.create();
HelpDerived* derived = dynamic_cast<HelpDerived *>(based);
std::cout << derived->a << " " << derived->b << std::endl;
delete derived;
}
I've replaced dynamic cast by static cast and it worked for me almost fine.
#include <iostream>
struct HelpBase {
int a = 0;
virtual void output() {}
};
struct HelpDerived : HelpBase {
int b = 0;
void output() override {}
};
class Base {
public:
virtual HelpBase create() = 0;
};
class Derived : public Base {
public:
HelpBase create() override;
};
HelpBase Derived::create() {
HelpDerived d;
d.a = 3;
d.b = 2;
return d;
}
int main() {
Derived d;
auto based = d.create();
HelpDerived derived = static_cast<HelpDerived &>(based);
std::cout << derived.a << std::endl;
std::cout << derived.b << std::endl;
}
Output:
3
32726
Note that a value is correct, while b is junk. This is because for derived constructor has not been called.
In general, casting from base class to derived is not recommended, as derived may have more data members, than base class.
Recommended reading: https://www.bogotobogo.com/cplusplus/upcasting_downcasting.php
Big edit:
I have a code in which I have to add a constant member in a inherited class by using _elemente (which is a vector). Not to add a member in the inherited classes, just by using _elemente. In every inherited classes (let's say B, C, D and E) I withh have MAX_VAL1, MAX_VAL2 and so on with different values.
I tried:
#include <iostream>
#include <iomanip>
#include <vector>
typedef unsigned int Uint;
typedef vector<Uint> TVint;
typedef vector<Uint>::const_iterator TIterator;
class A
{
protected:
Uint _count;
TVint _elemente;
public:
//
};
class B : public A
{
const int MAX_VAL;
};
But it has a member and I don't have to have a member in the inherited class.
All the code here:
.h: http://pastebin.com/P3TZhWaV
.cpp: http://pastebin.com/ydwy2L5a
The work from the inherited classes is done using that constant members.
if MAX_VAL1 < count
{
throw Exception() {}
}
if (_elemente.size() == 0) // As _elemente is a vector from STL
{
_elemente.push_back(0);
}
for (int i = _elemente.size(); i < count; i++)
{
_elemente.push_back(_elemente[i * (i+1) / 2]);
}
}
I don't think that is correct as I have to use the Vector from STL and I don't really think that is the way the constant member from a inherited class without the actual member declared should be added.
Thanks for your help.
You could use a virtual function, something like this:
class A
{
virtual int max_val() const = 0;
protected:
Uint _count;
TVint _elemente;
public:
//
};
class B : public A
{
int max_val() const { return 42; }
};
if ( max_val() < _count ) ...
Based on other comments it seems like you want a const number that is accessible in the base class which can have a different value depending on the derived class. You could achieve that like this: https://ideone.com/JC7z1P
output:
A: 50
B: 80
#include <iostream>
using namespace std;
class Base
{
private:
const int aNumber;
public:
// CTOR
Base( const int _aNumber ) :
aNumber( _aNumber ) {}
// check value
int getNumber() const
{
return aNumber;
}
};
class A : public Base
{
public:
A() : Base( 50 ) {}
};
class B : public Base
{
public:
B() : Base( 80 ) {}
};
int main() {
A a;
B b;
std::cout << "A: " << a.getNumber() << std::endl;
std::cout << "B: " << b.getNumber() << std::endl;
return 0;
}
When you write like
class B : public A
{
const int MAX_VAL;
};
what value do you expect B's class instance to hold with current approach?
Have you tried to add ctor to B (to initialize MAX_VAL to some exact value), so that whole class definition should be like
class B : public A
{
const int MAX_VAL;
public:
B(int max_val):MAX_VAL(max_val) {}
};
Also, the code above shows a lot of unanswered questions. Some of them:
Do you really need it to be member? mark it as 'static' (static const int MAX_VAL = 5) . That would mean, every B's instance MAX_VAL would be equal
All of type redifinitions don't look meaningful. What if you use intrisic types and auto?
Usually one doesn't compare size() with 0 - just calls empty().
Have you tried to read Stroustrup or Lippman?
If you want to access it statically, you can do it by using templates :
ABase gives polymorphic access to value
A gives static access to value
B and Care examples of usage
.
// This is the polymorphic root class
class ABase
{
public:
ABase(int maxV) : _maxV(maxV) {}
int maxValue() { return _maxV; }
private:
int _maxV;
};
// This class gives static method
template<int V_MaxValue>
class A : public ABase
{
public:
A() : ABase(V_MaxValue) {}
static int maxValue() { return V_MaxValue; }
};
class B : public A<42>
{
};
class C : public A<35>
{
};
// Static access (neex explicit class call) :
// B::maxValue() => 42
// C::maxValue() => 35
//
// Polymorphic call :
// ABase* poly = new B();
// poly->maxValue() => 42
If I have two classes:
class A{
f();
}
class B{
f();
};
I need to assign one of these classes to an object based on a condition like:
define variable
if condition1
variable = A
else
variable = B
and then I would use the assigned variable.f();
You should look toward inheritance and virtual functions.
Code might look like
class Base
{
virtual void f() = 0;
};
class A : public Base
{
virtual void f()
{
//class A realization of f
}
};
class B : public Base
{
virtual void f()
{
//class B realization of f
}
};
And then you can do this
Base* VARIABLE = 0;
if (*condition*)
{
VARIABLE = new A();
}
else
{
VARIABLE = new B();
}
VARIABLE->f();
But it not always a good idea to use inheritance and virtual functions. Your classes A and B should have something in common, at least the meaning of function f().
Provided A and B are meant to be unrelated types (i.e. not part of an inheritance hierarchy), you could use Boost.Variant in combination with the boost::static_visitor<> class to achieve something similar:
#include <boost/variant.hpp>
#include <iostream>
struct A { void f() { std::cout << "A::f();" << std::endl; } };
struct B { void f() { std::cout << "B::f();" << std::endl; } };
struct f_caller : boost::static_visitor<void>
{
template<typename T>
void operator () (T& t)
{
t.f();
}
};
bool evaluate_condition()
{
// Just an example, some meaningful computation should go here...
return true;
}
int main()
{
boost::variant<A, B> v;
if (evaluate_condition())
{
A a;
v = a;
}
else
{
B b;
v = b;
}
f_caller fc;
v.apply_visitor(fc);
}
What you are doing is known in design patterns as the "Factory Pattern". The above answers cover how it should be implemented. You can get more information at How to implement the factory method pattern in C++ correctly and wiki (http://en.wikipedia.org/wiki/Factory_method_pattern).
Code:
struct Base { ... };
struct A : public Base { ... };
struct B : public Base { ... };
struct C : public Base { ... };
Is it possible to create an array, that holds that types of struct?
sample/expected result:
Type inheritedTypesOfStruct[3] = {A, B, C};
The purpose of this is that I later want to create an object with a random class retrieved from the array.
You could create an array of functions, each of which returns a base pointer(or smart pointer) that each point to objects of your various derived classes. e.g.
typedef std::unique_ptr<Base> base_ptr;
template<typename Derived>
base_ptr CreateObject()
{
return base_ptr(new Derived);
}
int main()
{
std::function<base_ptr(void)> f[3] = {
CreateObject<A>, CreateObject<B>, CreateObject<C>
};
base_ptr arr[10];
for (int i=0; i<10; ++i)
arr[i] = f[rand()%3]();
}
Here it is in action: http://ideone.com/dg4uq
If your compiler supports RTTI, you can do something like:
const type_info *inheritedTypesOfStruct[3] = {
&typeid(A), &typeid(B), &typeid(C)
};
However, you won't be able to instantiate a class using only its type_info. The factory pattern might be a better answer to your root problem.
Update: Since type_info instances cannot be copied (their copy constructor and assignment operator are private), and arrays of references are illegal, constant pointers have to be used in the example above.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <vector>
#include <memory>
using namespace std;
// interface
class Base
{
public:
virtual ~Base() { }
virtual int getClassId() = 0;
};
// class A relizes interface Base, has ID == 1 (is used in automatic registration to factory)
class A : public Base
{
public:
const static int ID = 1;
static Base* CreateInstance()
{
return new A();
}
virtual int getClassId()
{
return ID;
}
virtual ~A() { }
};
// class B relizes interface Base, has ID == 2 (is used in automatic registration to factory)
class B : public Base
{
public:
const static int ID = 2;
static Base* CreateInstance()
{
return new B();
}
virtual int getClassId()
{
return ID;
}
virtual ~B() { }
};
// this is the objects factory, with registration only (unregister s not allowed)
class ObjectFactory
{
ObjectFactory() { }
ObjectFactory(ObjectFactory&) { }
public:
virtual ~ObjectFactory() { }
static ObjectFactory& instance()
{
static ObjectFactory objectFactory;
return objectFactory;
}
typedef Base* (*Creator) ();
void registerCreator(int id, Creator creator)
{
registry[id] = creator;
}
Base* CreateById(int id)
{
return registry[id]();
}
private:
map<int, Creator> registry;
};
// this template class is used for automatic registration of object's creators
template <class T>
struct RegisterToFactory
{
RegisterToFactory(ObjectFactory& factory)
{
factory.registerCreator(T::ID, &T::CreateInstance);
}
};
namespace
{
// automaticaly register creators for each class
RegisterToFactory<A> autoregisterACreator(ObjectFactory::instance());
RegisterToFactory<B> autoregisterBCreator(ObjectFactory::instance());
}
// lets this this solution
int main(int argc, char *argv[])
{
vector<int> ids;
ids.push_back(static_cast<int>(A::ID));
ids.push_back(static_cast<int>(B::ID));
srand(time(0));
for (int i = 0; i < 20; ++i)
{
int randomClasssId = ids[rand() % ids.size()];
auto_ptr<Base> testObject(ObjectFactory::instance().CreateById(randomClasssId));
cout << "Object of classId = " << testObject->getClassId() << " has been produced by factory." << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
I don't get the question. Are you asking for an array that can hold different type of instances at the same time? That is possible using polymorphism, of course. Or are you trying to get an array of types (like reflection)? That would be possible using RTTI or Qt type information (as an example), but I never did that.
You can take a look here: http://www.java2s.com/Code/Cpp/Class/Objectarraypolymorphism.htm
on how to use Polymorphism in C++.