473,404 Members | 2,187 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,404 software developers and data experts.

accessing derived methods via base class (design woes)

Consider

# include <iostream>
# include <vector>
# include <typeinfo>
# include <string>

class BaseMsg {
friend std::ostream& operator<<( std::ostream&, const BaseMsg& );
std::string name ;
public :
std::string get_name() const { return name ; }
BaseMsg( std::string const & str_ )
: name ( str_ )
{}
virtual std::ostream& print( std::ostream& os ) const { return os <<
"Base"; }
};
inline std::ostream& operator<<(std::ostream& os, const BaseMsg& b) {
return b.print(os);
}

class msg41K : public BaseMsg {
int const ac ;
public :
int ac_type () const { return ac; }
std::ostream& print(std::ostream& os) const {
return os << "msg41K" << std::endl;
}
explicit msg41K ( int const ac_ )
: BaseMsg ( "msg41K" )
, ac ( ac_ )
{}
};
class msg42K : public BaseMsg {
int const cd ;
public :
int code_digit () const { return cd; }
std::ostream& print(std::ostream& os) const {
return os << "msg42K" << std::endl;
}
explicit msg42K ( int const cd_ )
: BaseMsg ( "msg42K" )
, cd ( cd_ )
{}
};

// Event delegate interface class
struct EvhDelegate {
typedef void ( EvhDelegate::*handler_fp )( BaseMsg& );
virtual void handle( BaseMsg& ) = 0;
virtual ~EvhDelegate(){}
};

// ListenerA event handler
struct ListenerA : EvhDelegate {
void handle( BaseMsg& e ) {
//e.print( typeid ( e ).name() ) ;
//std::cout << typeid ( e ).name() << std::endl; OR
std::cout << e.get_name() << std::endl;

}
};

// ListenerB event handler
struct ListenerB : EvhDelegate {
void handle( BaseMsg& e ) {
std::cout << typeid ( e ).name() << std::endl;
}
};

// Our functor (modified to handle polymorphic functions)
template < typename classT, typename memfuncT >
class functor {
memfuncT memfunc_;
classT * class_;
public:
functor()
: class_ ( 0 )
, memfunc_( 0 )
{}

functor(classT * c, memfuncT f)
: class_( c )
, memfunc_( f )
{}

void operator()( BaseMsg& e ) {
if( class_ && memfunc_ ) {
(class_->*memfunc_)( e );
}
}
~functor() { delete class_; }
};

//////
///
//////
class Subject : public BaseMsg {
public :
std::ostream& print(std::ostream& os) const {
return os << "Subject" << std::endl;
}
};
// Friendly typedef of our functor for this specific usage
typedef functor<EvhDelegate, EvhDelegate::handler_fpEvhFunctor;
class EventHandler {

typedef std::vector<EvhFunctor *handler_t;
typedef handler_t::const_iterator hciter;
typedef handler_t::iterator iter;
handler_t handler_;

public:
// Log events and handlers in a map
void Attach(EvhFunctor * func) {
handler_.push_back( func );
}

// Extract handler from map and call polymorphic handle() function
// Gets called in the context of the base class but dynamic binding
ensures
// the handler for the correct delegate class is called.
void Notify( BaseMsg& e ) {
for( hciter itr = handler_.begin() ; itr != handler_.end() ; +
+itr) {
EvhFunctor * func = (*itr);
( *func )( e );
}
}
// Clear up the handlers
~EventHandler() {
for( hciter itr = handler_.begin() ; itr != handler_.end() ; +
+itr ) {
delete *itr;
}
}
};
int main() {

BaseMsg* ptr_41 = new ( std::nothrow ) msg41K ( 1 ) ;
BaseMsg* ptr_42 = new ( std::nothrow ) msg42K ( 2 ) ;

EventHandler eh;
eh.Attach(new EvhFunctor( new ListenerA, &EvhDelegate::handle ) );
eh.Attach(new EvhFunctor( new ListenerB, &EvhDelegate::handle ) );

eh.Notify( *ptr_41 );
eh.Notify( *ptr_42 );

}

The listeners upon receipt of msg41 or msg42 the Listeners prints the
appropriate message. It's clear from the code that the messages are
expressed to the Listeners via BaseMsg. Trouble is, I'd like for
each Listener to first determine the type (msg41 or msg42 ), then
print via cout "code_digit" or "ac_type" depending on the appropriate
message.

Since each message (msg41, msg42 .. msgNN) has unique methods, it
doesn't make sense for me to create put every single method - pure
virtual form - in the BaseMsg.

The question then becomes. What is an alternate approach?

Thanks
Sep 17 '08 #1
0 1243

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bryan Ray | last post by:
I am trying to write an inheritance function, so I can call a base classes method that has been overridden in the derived class. I want to get rid of the ugly 'call()' syntax that would be used....
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
6
by: Edward Diener | last post by:
Since a C++ using declaration isn't allowed in MC++, is there a way to specify that a property, method, event, or field's access can be changed in a derived class, ie. is protected in one class and...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
2
by: Ramashish Baranwal | last post by:
Hi, I want to access a static variable in a staticmethod. The variable can be redefined by derived classes and that should be reflected in base's staticmethod. Consider this trivial example- ...
4
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.