473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

casting base class ptr into derived to call function ptr

10
Hello,

I have a layout (simplified) as follows :

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class MsgHandler
  3. {
  4. protected :
  5.     typedef void (T::*handle_t)(Message *);
  6.     handle_t _handles[128];
  7. public :
  8.     void procMsg()
  9.     {
  10.                 Message * msg = EventMgr::GetMessage();
  11.         (reinterpret_cast<T *>(this)->*_handles[msg->evt()])(msg);
  12.     }
  13. };
  14.  
  15. class Amgr : public MsgHandler<Amgr>
  16. {
  17. public :
  18.     Amgr():MsgHandler(TGT_ALPHA)
  19.     {
  20.         _handles[ALPHA_SET_VAL] = &Amgr::OnChangeInt;
  21.     }
  22.  
  23.     void OnChangeInt(Message * msg)
  24.     {
  25.         ...
  26.     }
  27. };
Question :

I'm worrying about procMsg() in MsgHandler, is it safe to cast the 'this' pointer into the derived class, in order to call a function which *might* affect the derived class variables??

It's the only way I've found that the base class can use the derived class functions, without actually knowing about them and without writing the same code for every class that inherits from MsgHandler.

Thanks in advance,
babis
Feb 4 '08 #1
2 1890
weaknessforcats
9,208 Expert Mod 8TB
The base class can call a derived class method without using a cast if you use
the Hollywood Principle: Don't call us, we'll call you.

This is a design pattern called the Template Method. Here the process is managed
by the base class and tailored by the derived class. It works becaause the access
specifiers (public/private/protected) are ignored when you override functions.

Check this out:
Expand|Select|Wrap|Line Numbers
  1. class Parent
  2. {
  3.     private:
  4.         virtual void MethodB() = 0;
  5.     public:
  6.         void MethodA();
  7.  
  8.  
  9. };
  10. void Parent::MethodA()
  11. {
  12.     this->MethodB();
  13. }
  14. class Child : public Parent
  15. {
  16.     private:
  17.         void MethodB();
  18. };
  19. void Child::MethodB()
  20. {
  21.     cout << "You have called Child::MethodB()" << endl;
  22. }
  23. int main()
  24. {
  25.      Parent* obj = new Child;
  26.      obj->MethodA();
  27. }
  28.  
You creat a Child object. The MethodA() that is called is to the Parent object
embedded in the Child. That method calls the virtual MethodB(), which is overridden
by the Child MethodB() so it is the Child MethodB() that is actually called.

The effect is the Parent class called a Child class method.

This design pattern separates the the interface from the implementation.
The interface being the public Parent functions and the implementation being the virtual functions.
This leads to private virtual functions (remember access specifiers are ignored for overriding),
which is the preferred way to design for polymorphism today.
Feb 4 '08 #2
esrever
10
Very interesting pattern, I didn't know..

Problem solved, although all the derived classed now need to override the pure-virtual with the *exact* same piece of code, and all this because of the function pointer requirements, but whatever.. Thanks again!
Feb 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: James Brown | last post by:
I have two classes, a base class, and a class base { public: base(); virtual void foo() = 0; }; class derived : public base
2
by: hswerdfe | last post by:
I have several Classes that derive from a common Base Class Each of these Classes Has a Differnt Version of a Function with the Same Short name, but different Long Name. See Bellow: // Base...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
10
by: Brett Romero | last post by:
Say I have a class inheriting some base class: BaseClass { void Foo() { Update(); } }
8
by: Raider | last post by:
I have simple class hierarchy (without multiple inheritance): class Base {}; class Derived : public Base {}; class DeepDerived : public Derived {}; // ... a lot of types Is it ok to cast...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.