Calling different child class function from the same parent invocation - c++

Consider this trivial C++11 inheritance example:
class A
{
public:
virtual void func() = 0;
};
class B : public A
{
public:
void func() { func1(); /* Wish this could be func1() or func2()! */ };
void func1() { /* Does one thing */ };
void func2() { /* Does another thing */ };
};
void doSomeStuff(A &a)
{
a.func();
}
int main()
{
B b;
doSomeStuff(b);
return 0;
}
I'm trying to make it so that I don't have to modify (or duplicate) class A's definition or the function doSomeStuff, but I want the invocation of a.func() to call either func1() or func2() of B. Ideally I'd change the line doSomeStuff(b) to something like doSomeStuff(b.butWithFunc1) but I'd also be OK with some way to modify B's version of func() so that it can make the decision internally to call func1 or func2 based on some parameter.
The same object of type B may have to sometimes call func1 or func2 during an invocation of func, so I can't use a persistent member of class B to decide. Adding a parameter to func() would make this trivial as well, but that's not something I can do either.
I'm kind of wondering if there's some way to add to class B a function that returns a mutated version of class B which calls func2() from func(), or if I can play some tricks with function pointers or something. However, something tells me I'm Doing It Wrong and the obvious solution is staring me in the face.
If it helps for context, class A is similar to a std::lock_guard, and it works fine for things like semaphores and mutexes (for which there is only one definition of lock and unlock), but class B in this example is a R/W lock - so there's a "readLock" and "writeLock", and I'd like to be able to say something like "auto lock this RW lock as a read lock" without having to duplicate/break the auto lock code.
For instance:
{
A_AutoSem(myMutex); // calls lock() on myMutex
//... do some stuff
// end of the block, ~A_AutoSem calls unlock on myMutex
}
{
A_AutoSem(B_RWLock); // how do I say here "call readLock"?
// ... do some stuff
// end of the block ~A_AutoSem should call "readUnlock" on B_RWLock
}

Simply define some additional classes to call func1() and func2(), and then pass those classes to doSomeStuff() instead of passing B directly.
Try something like this:
class A
{
public:
virtual void func() = 0;
};
class B
{
public:
void func1() { /* Does one thing */ };
void func2() { /* Does another thing */ };
};
class C1 : public A
{
private:
B &m_b;
public:
C1(B &b) : m_b(b) {}
void func() override { m_b.func1(); }
};
class C2 : public A
{
private:
B &m_b;
public:
C2(B &b) : m_b(b) {}
void func() override { m_b.func2(); }
};
void doSomeStuff(A &a)
{
a.func();
}
int main()
{
B b;
{
C1 c(b);
doSomeStuff(c);
}
{
C2 c(b);
doSomeStuff(c);
}
return 0;
}
Live Demo
Alternatively:
class A
{
public:
virtual void func() = 0;
};
class B
{
private:
void func1() { /* Does one thing */ };
void func2() { /* Does another thing */ };
public:
class C1 : public A
{
private:
B &m_b;
public:
C1(B &b) : m_b(b) {}
void func() override { m_b.func1(); }
};
class C2 : public A
{
private:
B &m_b;
public:
C2(B &b) : m_b(b) {}
void func() override { m_b.func2(); }
};
};
void doSomeStuff(A &a)
{
a.func();
}
int main()
{
B b;
{
B::C1 c(b);
doSomeStuff(c);
}
{
B::C2 c(b);
doSomeStuff(c);
}
return 0;
}
Live Demo

Related

Optional virutal function in extended class, test if it exists at runtime

class A {
public:
void (*_start)() = 0;
void start() {
cout<<"Le ";
if (_start!=0) _start();
}
A() {}
};
class B : public A {
public:
void _start() {
cout<<"Blop";
}
B(){}
};
class C : public A {
public:
C(){}
};
int main () {//Desired results:
B().start(); //Le Blop
C().start(); //Le
return 0;
}
The following code above does not run, but in the main method I have thier desired outputs.
Is it at all possible to have "optional" virtual methods? To be able to easily overload and impliment a function in the child class, and have it as some null value in the parent class so that its methods can identify if the function is defined or not.
What I get from the code above is Le Le, I assume _start in the parent class differs from the _start in the child class, not by value, but identity.
If so, how is this done? I would like to do it in a tidy way, such as, not having to set _start in the constructor.
Was alittle silly,
I could simply define the optional function as virtual void _start() { }; instead of virtual void _start() = 0;. Having the exact same behavior of the desired code above
Resulting in:
class A {
public:
virtual void _start() { };
void start() {
cout<<"Le ";
_start();
}
A() {}
};
class B : public A {
public:
void _start() {
cout<<"Blop";
}
B(){}
};
class C : public A {
public:
C(){}
};
int main () {//Desired results:
B().start(); //Le Blop
C().start(); //Le
return 0;
}
Without virtual methods, you might do:
class B : public A {
public:
B() {
_start = [](){ std::cout<<"Blop\n";};
}
};
Demo
But you don't need inheritance in fact:
int main () {
A a1{[](){ std::cout<<"Blop\n";}};
A a2;
a1.start(); //Le Blop
a2.start(); //Le
}
Demo
void (*_start)() could be replaced by std::function<void()> _start; to allow more functors (as capturing lambda).
The classes B and C don't exist without class A and class A is part of B and C respectively. With this in mind, class A shouldn't worry if it is inherited by other classes.
With this in mind, you can simplify the code. Below you can find an example houw the code could be:
struct A {
void start() {
cout << "Le ";
}
};
struct B : public A {
void start() {
A::start();
cout << "Blop\n";
}
};
struct C : public A {
void start() {
A::start();
}
};
int main () {
B b;
C c;
b.start();
c.start();
return 0;
}

c++ how to implement a switch between class members

I am very new to c++ so I am trying to get a feeling of how to do things the right way in c++. I am having a class that uses one of two members. which one gets determined at instantiation. It looks something like
main() {
shared_pointer<A> a = make_shared<A>();
if ( checkSomething ) {
a->setB(make_shared<B>());
} else {
a->setC(make_shared<C>());
}
a->doStuff();
class A {
public:
doStuff() {
/*here I want to do something like call
m_b->doStuff() if this pointer is set and m_c->doStuff() if
that pointer is set.*/
}
setB( B* p ) { m_b = p; }
setC( C* p ) { m_c = p; }
B* m_b;
C* m_c;
}
}
B and C are some classes with doStuff() member function
There are many members like doStuff. Ideally I would avoid checking for nullptr in each of them. What is the best/most efficient/fastest way to create a switch between those two members?
Is there a way to use a static pointer so that I have a member
static **int m_switch;
and do something like
m_switch = condition ? &m_b : &m_c;
and call
*m_switch->doStuff();
Does the compiler here also replace the extra pointer hop because it is a static?
Is there any other smart way to do those switches?
Normally, class A would be an interface class, which both B and C would inherit and implement. But it sounds like you cannot do this for whatever reason.
Since you want to emulate this, you can start by making the interface:
class A_interface
{
public:
virtual void doStuff() = 0;
virtual void doThings() = 0;
virtual void doBeDoBeDo() = 0;
};
And then you make a template wrapper:
template< class T >
class A : public A_interface
{
public:
void doStuff() override { target.doStuff(); }
void doThings() override { target.doThings(); }
void doBeDoBeDo() override { target.doBeDoBeDo(); }
private:
T target;
};
This essentially does half of what your own example class A was trying to do, but now you can use a common interface. All you need to do is construct the correct templated version you want:
std::shared_ptr<A_interface> a;
if( checkSomething ) {
a = std::make_shared<A<B>>();
} else {
a = std::make_shared<A<C>>();
}
a->doStuff();
You need to have both members implement a common interface to use them similarly. But in order to do that, you need to define the interface and relay the calls to the B and C classes.
// existing classes
class B
{
public:
void doStuff() { std::cout << "B"; }
};
class C
{
public:
void doStuff() { std::cout << "C"; }
};
// define your interface
class I
{
public:
virtual void doStuff() = 0;
};
// new classes
class D : public B, public I
{
public:
void doStuff() override { B::doStuff(); }
};
class E : public C, public I
{
public:
void doStuff() override { C::doStuff(); }
};
// your A class
class A
{
public:
D* b = nullptr; // now type D
E* c = nullptr; // now type E
// your toggle
I* getActive()
{
if (b)
return b;
else
return c;
}
// simple doStuff() function
void doStuff()
{
getActive()->doStuff();
}
};
int main()
{
A a;
if (true)
a.b = new D; // need to initialize as D
else
a.c = new E; // need to initialize as E
a.doStuff(); // prints B
}
But typing this up made me realize that defining D and E could get really tiresome and against what you're trying to save. However, you can define a template to create them like #paddy has done.
There's no one-size-fits-all solution for your problem. What to use depends on your particular problem. A few possible answers:
Interfaces
Strategy Pattern
Pointers (to hold a function or class which implements doStuff)
An interface is like a contract. Any class which inherits from the interface must implement its members. For instance,
class IDoesStuff
{
public:
virtual ~IDoesStuff() {};
virtual void DoStuff() = 0;
};
Can now be used by other classes:
class Foo : public IDoesStuff
{
public:
virtual void DoStuff()
{
// ....
}
};
class Bar : public IDoesStuff
{
public:
virtual void DoStuff()
{
// ....
}
};
And now, in general, one may do:
Foo foo;
IDoesStuff *stuffDoer= &foo;
stuffDoer->doStuff();
This can be used in your particular use case as follows:
class A
{
IDoesStuff *stuffDoer; // Initialize this at some point.
public:
void doStuff() { stuffDoer->doStuff(); }
};
First you must change your memebr variables m_b and m_c to std::shared_ptr.
Add a member variable of type std::function(void()) to hold the target function you want to call. In your sample it is do_stuf.
In your setter functions you can bind target function to your std::function and in do_stuf just call std::function.
(You need a C++11 compiler)
class B
{
public:
void doStuff()
{
}
};
class C
{
public:
void doStuff()
{
}
};
class A
{
public:
void doStuff()
{
m_target_function();
}
void setB(std::shared_ptr<B> p)
{
m_b = p;
m_target_function = std::bind(&B::doStuff, m_b.get());
}
void setC(std::shared_ptr<C> p)
{
m_c = p;
m_target_function = std::bind(&C::doStuff, m_c.get());
}
std::shared_ptr<B> m_b;
std::shared_ptr<C> m_c;
std::function<void()> m_target_function;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::shared_ptr<A> a = std::make_shared<A>();
bool use_B = false;
if (use_B)
{
a->setB(std::make_shared<B>());
}
else
{
a->setC(std::make_shared<C>());
}
a->doStuff();
}

two classes within a class

What is a better way to implement this in C++ (without if-else). In C I can initialize function pointer, and then just call function pointers. So if you look at class C, some objects of class C use only class A and some class B. I am more familiar with C than C++.
class A {
public:
int func1();
int func2();
int func3();
};
class B:A {
public:
int func1() {x=1; A::func1();}
int func2() {A::func2(); x=1;}
int func3() {x=1; A::func2();}
};
class C {
public:
C::C(bool class_a) {_class_a = _class_a;}
void func_c_1()
{
if(_class_a) a.func1();
else b.func1();
}
void func_c_2()
{
if(_class_a) a.func2();
else b.func2();
}
void func_c_3()
{
if(_class_a) a.func3();
else b.func3();
}
private:
bool _class_a;
A a;
B b;
};
Your code doesn't compile and I could be misunderstanding you completely but if you want a class which does different stuff depending on how it was created you might be helped by using a virtual base with pure virtual functions and deriving A and B from it overriding those virtual functions.
class Base
{
public:
virtual int func1() = 0;
virtual int func2() = 0;
virtual int func3() = 0;
virtual ~Base(){} //it is important that this is virtual
};
class A: public Base
{
public:
virtual int func1(){
//do something
}
virtual int func2(){
//do something
}
virtual int func3(){
//do something
}
};
class B: public A
{
public:
virtual int func1(){
//do something else
((A*)this)->func1(); //call A's func1()
}
virtual int func2(){
//do something else
}
virtual int func3(){
//do something else
}
};
Base *a = new A;
Base *b = new B;
Base *c = a;
c->func1(); //does something
c=b;
c->func1(); //does something else
Otherwise if you essentially want two different classes (with distinct types) you could use a template. If this is what you want comment and I will add example code.
It also should be noted that nothing is stopping you from using function pointers in C++ although I wouldn't recommend it. Here is an example code just for giggles:
#include <functional>
class A
{
public:
int func1(){
//do something
return 1;
}
int func2(){
//do something
return 1;
}
int func3(){
//do something
return 1;
}
};
class B
{
public:
int func1(){
//do something else
return 1;
}
int func2(){
//do something else
return 1;
}
int func3(){
//do something else
return 1;
}
};
class C
{
public:
C(bool useA)
{
if(useA) {
func1 = std::bind(&A::func1,a_);
func2 = std::bind(&A::func2,a_);
func3 = std::bind(&A::func3,a_);
}
else {
func1 = std::bind(&B::func1,b_);
func2 = std::bind(&B::func2,b_);
func3 = std::bind(&B::func3,b_);
}
}
std::function<int()> func1;
std::function<int()> func2;
std::function<int()> func3;
private:
A a_;
B b_;
};
C c(true);
C c2(false);
c.func1();
It looks to me like you're trying to understand inheritance in C++. Your code looks like it's trying to get different behavior depending on if you tell it to be a class A or a class B. If so, this should help:
#include <iostream>
using namespace std;
class A
{
public:
void foo() { cout << "A::foo\n"; }
void bar() { cout << "A::bar\n"; }
};
class B : public A
{
public:
void foo() { cout << "B::foo\n"; }
void fee() { cout << "B::fee\n"; }
};
main()
{
A* a = new A();
B* b = new B();
a->foo();
a->bar();
b->foo();
b->bar();
b->fee();
((a)b)->foo();
((a)b)->bar();
}
I make class A, and (publicly) make class B inherit from it. Within main, the first 5 lines show behavior you'd expect, but if you want the b object to act as an A, I just cast it to it, and it does. (If you run this code, you'll see that the final foo line emits A::foo.)
So the behavior is not forced at construction time. When you construct the object, B::foo overrides A::foo. But simply by casting to the superclass, you can access the original functions.
Note, if you want b.foo() to always emit "B::foo", set the foo function in A to virtual:
class A
{
public:
virtual void foo() { cout << "A::foo\n"; }
void bar() { cout << "A::bar\n"; }
virtual ~foo() { /* any destructor code for the base class */ }
};
In this case, whether you use b->foo() or ((A*)b)->foo(), the output will be "B::foo". The run time will work with the original type of the object, instead of with the type you cast it to. Important - As soon as you make any virtual function in your class, you must make a virtual destructor for that function. If you don't, only the base class's destructor will be called when your object is deleted. (When you use virtual destructors, each destructor is called from the most-derived class to the base, essentially unwinding the work that was done when the object was constructed.)
Hope this helps...

Call a method from A class in constructor of other class

I want to call a method from A class in constructor of other class
I googled, but did not find any answer
For example, I have :
class A{
void doWork();
}
class B{
B(){
//here i want to have doWork method
}
}
You told us not enough to choose proper solution. Everything depends on what you are trying to achieve. A few solutions:
a) Mark A method as static.
class A
{
public:
static void DoSth()
{
// Cannot access non-static A members here!
}
};
class B
{
public:
B()
{
A::DoSth();
}
};
b) You can instantiate A in place
class A
{
public:
void DoSth()
{
// Do something
}
};
class B
{
public:
B()
{
A a;
a.DoSth();
}
};
c) You can put A's instance into B:
// A remains as in b)
class B
{
private:
A a;
// or: A * a;
public:
B()
{
a.DoSth();
// or: a = new A; a->DoSth();
// Remember to free a somewhere
// (probably in destructor)
}
}
d) You may derive B from A:
class A
{
protected:
void DoSth()
{
}
};
class B : public A
{
public:
B()
{
DoSth();
}
};
e) You can forget about A class and make DoSth a function:
void DoSth()
{
// ...
}
class B
{
public:
B()
{
DoSth();
}
}
Since you provided not enough data, you have to choose solution on your own.
In order for that to work you'd need to subclass it.
So it'd be like this:
class A {
doWork();
}
class B : A {
B(){
doWork();
}
}
You could also do it like so going for a HAS-A rather than IS-A relationship:
class A {
doWork();
}
class B {
A myA;
B(){
myA.doWork();
}
}
Without knowing more of what you are doing I'd go with the top (IS-A) solution which is what I think you are trying to do.
Or
class A
{
public:
static void doWork();
};
class B
{
B(void)
{
A::doWork();
}
};
?
PS: Here B::B() will be private

C++ Call private / protected function of a common base class

Is there a nice way to call A::foo() from B::bar() in the following sample?
class A {
protected:
void foo() {}
};
class B : public A {
public:
void bar(A& a) { // edit: called with &a != this
a.foo(); // does not work
}
};
I can't think of anything other than declaring B to be a friend of A, but that could get pretty ugly with some more classes.
Any ideas?
Yes, you can use a base-class function.
class A {
protected:
void foo() {}
void do_other_foo(A& ref) {
ref.foo();
}
};
class B : public A {
public:
void bar(A& a) { // edit: called with &a != this
this->do_other_foo(a);
}
};
Why are you passing object of type A? You could do like this :
class B : public A {
public:
void bar() {
foo();
}
};
or, like this
class B : public A {
public:
void bar() {
A::foo();
}
};
Here's an approach to giving "protected" like access, allowing calls by any derived classes or object.
It uses a protected token type, required to un-lock privileged methods:
struct A
{
protected:
//Zero sized struct which allows only derived classes to call privileged methods
struct DerivedOnlyAccessToken{};
public: //public in the normal sense :
void foo() {}
public: //For derived types only :
void privilegedStuff( DerivedOnlyAccessToken aKey );
};
struct B: A
{
void doPrivelegedStuff( A& a )
{
//Can create a token here
a.privilegedStuff( DerivedOnlyAccessToken() );
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
a.foo();
a.privilegedStuff( A::DerivedOnlyAccessToken() ); // compile error.
B b;
b.doPrivelegedStuff( a );
return 0;
}
This is not my idea. I read it some place. Sorry I dont recall who's cunning idea it was.
I expect the compiler can elide the aKey parameter.