I have 3 classes: the first a pure virtual base class base with derived classes derived1, derived2...
the second foo holds a smart ptr to a base class
the third holds a smart ptr to foo and has an overloaded member function which I want to depend on the derived class the ptr in foo points to.
Here is the working code
#include<iostream>
#include<boost/shared_ptr.hpp>
using namespace std;
class base{
public:
base(){}
~base(){}
virtual void FuncDer()=0;
};
class derived1 : public base{
derived1(){}
virtual void FuncDer(){
cout<<"derived function1!"<<endl;
}
};
class derived2 : public base{
derived2(){}
virtual void FuncDer(){
cout<<"derived function2!"<<endl;
}
};
class foo {
public:
foo();
~foo();
boost::shared_ptr<base> get_base_ptr(){
return base_obj;
}
private:
boost::shared_ptr<base> base_obj;
};
class bar {
public:
bar();
~bar();
void callfunc(){
func(foo_ptr->get_base_ptr());
}
private:
boost::shared_ptr<foo> foo_ptr;
void func(boost::shared_ptr<derived1>);
void func(boost::shared_ptr<derived2>);
};
int main()
{
cout<<"test"<<endl;
return 0;
}
This however seems to fail with the error no matching member function. So my question is firstly is there something obviously wrong with this approach and secondly is there a more efficient / object oriented design approach which better deals with this type of problem.
In principle this is the right way of doing it: Call the function on the base and polymorphism will do the right thing.
However, your code won't compile: The func definitions in bar aren't valid (at least I'm not sure what you intended them for - what are they supposed to implement?) and the func call in callfunc dereferences the pointer twice.
If you need concrete fixes, give the exact error.
Related
This compiles OK, but gives runtime error on the b->hello() call. My intention is to use dynamic type of the b pointer and call Inherited::hello() version.
Help :-)
#include <iostream>
#include <memory>
using namespace std;
class Base {
public:
static shared_ptr<Base> factory();
string hello(){};
virtual ~Base() {};
protected:
Base(){};
};
class InheritedA : public Base{
friend class Base;
public:
string hello() {return "I am iA";}
protected:
InheritedA() {};
};
shared_ptr<Base> Base::factory() {
shared_ptr<Base> ptrB;
shared_ptr<InheritedA> ptrA;
ptrA = shared_ptr<InheritedA>(new InheritedA);
ptrB = dynamic_pointer_cast<Base>(ptrA);
return ptrB;
};
int main() {
shared_ptr<Base> b;
b = Base::factory();
cout << b->hello();
return 0;
}
You are forgetting one very important thing when it comes to polymorphism and overriding base-class functions: The functions must be virtual.
Since your function isn't virtual you call the Base::hello function, and since it doesn't return anything you will have undefined behavior, leading to the crash you experience.
You can ask the compiler to help you detect such cases by using the override specifier:
class InheritedA : public Base{
friend class Base;
public:
string hello() override // Tell the compiler that this function should
// override the function from the parent class
{return "I am iA";}
protected:
InheritedA() {};
};
If you use the override specifier the compiler will issue an error if the function is not virtual.
Besides marking the base class function virtual, you really should either return something, anything, from the function, or make it abstract:
class Base {
public:
static shared_ptr<Base> factory();
// Make the function a pure abstract function that must be overridden
virtual string hello() = 0;
virtual ~Base() {};
protected:
Base(){};
};
Obviously - virtual was missing in Base::hello() declaration, also override after InheritedA::hello() .
#include <iostream>
#include <memory>
using namespace std;
class Base {
public:
static shared_ptr<Base> factory();
virtual string hello(){};
virtual ~Base() {};
protected:
Base(){};
};
class InheritedA : public Base{
friend class Base;
public:
string hello() override {return "I am iA";}
protected:
InheritedA() {};
};
shared_ptr<Base> Base::factory() {
shared_ptr<Base> ptrB;
shared_ptr<InheritedA> ptrA;
ptrA = shared_ptr<InheritedA>(new InheritedA);
//some very complex stuff here
ptrB = dynamic_pointer_cast<Base>(ptrA);
return ptrB;
};
int main() {
shared_ptr<Base> b;
b = Base::factory();
cout << b->hello();
return 0;
}
Say I have a class:
class Foo{
public:
Foo(){
}
//Is it possible to create a function like this:
virtual Foo* createOb(){
//Should create a new Foo,Bar or Fiz, depending on the actual object type.
}
}
class Bar: public Foo{
public:
Bar(){
}
}
class Fiz: public Foo{
public:
Fiz(){
}
}
Is it possible to have a method createOb() in the base class, so when createOb() is called on an instance of one of the derived classes, that an instance of the derived class is created ?
Yes, it can be done, using CRTP.
Bu first, returning a raw pointer obtained from new is very dangerous. In c++ raw pointers should be used only when they do not have ownership of the pointed object. So I took the liberty to use unique_ptr:
struct Base {
virtual auto create_obj() -> std::unique_ptr<Base>
{
return std::unique_ptr<Base>{};
}
};
// abstract works too:
struct Base {
virtual auto create_obj() -> std::unique_ptr<Base> = 0;
};
template <class Derived>
struct Base_crtp : Base {
auto create_obj() -> std::unique_ptr<Base> override /* final */
{
return std::unique_ptr<Base>{new Derived{}};
}
};
struct D1 : Base_crtp<D1>
{
};
struct D2 : Base_crtp<D2>
{
};
And then:
auto b1 = std::unique_ptr<Base>{new D1{}};
auto b2 = std::unique_ptr<Base>{new D2{}};
auto new_d1 = b1->create_obj();
auto new_d2 = b2->create_obj();
Definitely YES!!!
When a method is declared virtual in base class, and called through the derived class object, then the derived class function gets called (Read vprt, vtable concept in c++).
#include <iostream>
using namespace std;
class A{
public:
virtual A* getobj(){
return new A();
}
};
class B: public A{
public:
B(){cout<<"B constructor"<<endl;}
virtual A* getobj(){
return new B();
}
};
int main()
{
A *a = new B();
A *second = a->getobj();
return 0;
}
In the above code, we are calling the getobj() function using class B's object.
Here the constructor of class B is called twice.
first, for new B() in main
secondly for getobj function call which again creates object of B
This is not an optimal solution, but it works.
In your .h
class Foo{
public:
Foo();
virtual Foo* createOb();
};
class Bar: public Foo{
public:
Bar();
};
class Fiz: public Foo{
public:
Fiz();
};
In your .cpp
#include "Header.h"
Foo::Foo() {}
Foo* Foo::createOb(){
if (dynamic_cast<Bar*>(this)) {
return new Bar();
}
else if (dynamic_cast<Foo*>(this)) {
return new Foo();
}
return nullptr;
}
Bar::Bar() {}
Fiz::Fiz() {}
As already suggested please consider a pure virtual method
No, this is not possible with "pure" inheritance. The classes must override createOb() member function in order to support cloning.
You can see why this is not possible by considering separate compilation of classes. An implementation of one-fits-all createOb() member function must be completed in isolation from Bar and Fiz, making it impossible for the base to know the type of its subclasses.
An implementation with a pure virtual function in the base is very common, though.
Another approach is to use Curiously Recurring Template Pattern (CRTP) to implement cloning. This article explains how it can be done.
I'm facing the following situation:
class Base{
virtual void Stuff() = 0;
};
class ConcreteA : public Base{
void Stuff() {};
};
class ConcreteB : public Base{
void Stuff() {};
};
class Context {
Base exec() {
ConcreteA conc_a();
return a;
}
};
Of course the compiler gives me an error since Base is abstract. However, I'd need that exec() return a Base type. I know that I can make the compiler happy using pointers or references, but since the object ConcreteA is created in exec(), returning by value is the best way to avoid dangling references or pointer with undefined ownership.
Is there a way to avoid using pointers or references to handle this kind of situations?
This looks like a perfect, simple use case for std::unique_ptr. Here is a C++14 example:
#include <memory>
#include <iostream>
class Base{
public:
virtual ~Base() {}
virtual void Stuff() = 0;
};
class ConcreteA : public Base{
void Stuff() { std::cout << "ConcreteA::Stuff\n"; };
};
class ConcreteB : public Base{
void Stuff() { std::cout << "ConcreteB::Stuff\n";};
};
class Context {
public:
std::unique_ptr<Base> exec() {
return std::make_unique<ConcreteA>();
}
};
int main()
{
Context c;
auto base_ptr = c.exec();
base_ptr->Stuff();
}
Memory is automatically deleted when base_ptr in main goes out of scope.
Note that I also made the Base destructor virtual. Perhaps you just left it out in your code example for the sake of brevity, but its importance should be stressed, and I think it should be kept even in short example code.
I have a basic base class and a couple of sub-classes which inherit from it.
Class Base{
public:
Base(){};
virtual ~Base();
virtual void foomethod()=0; //Marked as pure virtual
};
Class A : public Base{
A(){}; //Ctor
~A(){}; //Dtor
void foomethod(){ /* DO SOMETHING */ }
}
Class B : public Base{
B(){}; //Ctor
~B(){}; //Dtor
void foomethod(){ /* DO SOMETHING */ }
}
Use of the above is throwing an error in the following usage:
Base a = A();
Base b = B();
The compiler is complaining about the function foomethod() being pure virtual.
However, when the following is used, there is no problem.
A a = A();
B b = B();
I would have thought that the first instance should be accepted due to C++'s principles of polymorphism. I want to enforce a specific method implementation of the method in sub-classes. If it is marked as virtual only, there is not guarantee that it will be explicitly overrided.
There are several issues with your code, I'll try to get most of them:
You can't have a pure function without marking it as virtual
You can't create objects of an abstract Base class (there's no code for the methods)
When you write
Base b = A();
you're creating two objects and then assigning the A object to the Base b object. This cannot happen since Base is abstract but in cases where this can happen you could experience the slicing problem.
You marked the destructor as virtual in the derived classes but not in the Base (and no definition either), always mark it as virtual when using virtual polymorphism otherwise derived destructors will not be called
This is a more correct way of doing it:
#include <iostream>
using namespace std;
class Base{
public:
Base(){};
virtual ~Base() {};
virtual void foomethod()=0; //Marked as pure virtual
};
class A : public Base{
public:
A(){}; //Ctor
virtual ~A(){}; //Dtor
void foomethod(){ cout << "Hello from A"; }
};
class B : public Base{
public:
B(){}; //Ctor
virtual ~B(){}; //Dtor
void foomethod(){ /* DO SOMETHING */ }
};
int main() {
// Base obj; // Can't do that
Base *obj = new A();
obj->foomethod(); // A's one
delete obj;
return 0;
}
http://ideone.com/gvcL9S
Oh and there were some syntax errors here and there.. and a visibility problem if you intend to use those functions from the outside.. but I believe the excerpt was just a pseudocode one.
I have a linked list of Foo objects. Foo is a base class, which has several classes inherit from it. Say, classes A, B, and C.
I am cycling through this linked list and calling a method some_method, which has 3 definitions; one for each child class:
some_method(A a);
some_method(B b);
some_method(C c);
The linked list is generic, so it is of type Foo, as it has an assortment of A, B and C objects.
When I'm cycling through the linked list at current_element, calling some_method(current_element);, how can I make it call the right method? The compiler complained until I wrote a some_method that took the generic Foo, and it only calls into that method.
Depending on your requirements, you may want to consider using polymorphism. To do this, add a pure virtual method to your base node class, and move the corresponding methods to the derived classes.
class Foo
{
public:
virtual void some_method() = 0;
};
class A : Foo
{
public
virtual void some_method()
{
// move the body of some_method(A a) here
}
};
For this to work, your linked list will need Foo*, instead of Foo.
class Node
{
public:
Foo* foo;
Node* next;
};
// ...
Node* someNode = GetNode();
// Calls correct method - A::some_method, B::some_method, or C::some_method
someNode->foo->some_method();
If you can't put some_method in Foo/A/B/C, then you might want to look into the Visitor design pattern:
http://en.wikipedia.org/wiki/Visitor_pattern
This is the "double dispatch" problem. You can use the visitor pattern. Usually the Visitor is a base class so you can re-use this design for multiple problems.
#include <iostream>
class FooVisitor;
class Foo
{
public:
virtual void some_method() = 0;
virtual void visit(FooVisitor* v) = 0;
};
class A;
class B;
class FooVisitor
{
public:
virtual void visit(A* a){ std::cout << "A" << std::endl;}
virtual void visit(B* b){std::cout << "B" << std::endl;}
};
class A : public Foo
{
public:
virtual void some_method()
{
// move the body of some_method(A a) here
}
virtual void visit(FooVisitor* v) { v->visit(this);}
};
class B : public Foo
{
public:
virtual void some_method()
{
// move the body of some_method(A a) here
}
virtual void visit(FooVisitor* v) { v->visit(this);}
};
int main()
{
FooVisitor fv;
Foo* f1 = new A;
f1->visit(&fv);
Foo* f2 = new B;
f2->visit(&fv);
getchar();
}
Two ways:
1) the better way:
Reverse your design such that someMethod is a virtual method of the base class Foo and redefine it in the derived classes. As:
class Foo {
public:
virtual void someMethod() = 0;
};
class A {
public:
void someMethod() { /* implementation specific to A here */ };
};
class B {
public:
void someMethod() { /* implementation specific to B here */ };
};
class C {
public:
void someMethod() { /* implementation specific to C here */ };
};
Then calling the someMethod on a pointer to Foo will automatically call the method from the appropriate class. If that cannot be done because someMethod cannot be implemented as part of Foo or its derivatives (e.g. it needs access to private members of the class it is currently in in your design), then you might try to split this functionality apart into subproblems that can be put into virtual methods of these classes A B C.
2) the "I don't have a choice" way:
Use RTTI (Run-Time Type Identification), it is included in C++. It requires that your base class Foo has at least one virtual method. You need to #include <typeinfo>, then use typeid() on the pointer, it will return a type_info object, and you can compare its name() result with the class names A B and C. This isn't a very nice approach because it has more overhead and it breaks OOP design principles. But if that's the only option, it's fine.
RTTI is your friend here. The example given in the link will guide you further
You can call the method for the child class as a member method. For exampleA a = new A(); a.some_method() should call the correct the method. Within some_method() you can reference to object using keyword this.