473,387 Members | 1,766 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,387 software developers and data experts.

Making a class more generic [part 2]

I have a class (actually the same one described in part 1) which
implements a state machine. The state machine is inside a pimpl, and
looks a bit like this:

class foo::fooImpl
{
public:
foo(void);
void StartBloggs(const BloggsType& bloggsStuff)
{
bloggsStuff_ = bloggsStuff;
state_ = 1;
}
void StartJimmy(const JimmyType& jimmyStuff)
{
jimmyStuff_ = jimmyStuff;
state_ = 2;
}
void Exec(void)
{
switch (state_)
{
case 1:
execBloggs();
break;
case 2:
execJimmy();
break;
}
}
private:
int state_;
BloggsType bloggsStuff_;
JimmyType jimmyStuff_;
void execBloggs();
void execJimmy();
};

As before, I now want to make this code a bit more generic. The problem
I now have is that some callers may want to only use the Jimmy code, and
others only want to use the Bloggs code. But anyone who links my class
gets the overhead of having both Bloggs and Jimmy, whether needed or
not. (In my example above, I could probably dynamically allocate the
state data bloggsStuff_ and jimmyStuff_, but I'm more interested in not
linking the execBloggs() and execJimmy() methods.)

Is there any way of only linking the stuff the user of the class needs,
and still keeping the pimpl in some shape or form?
--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #1
2 1233
Simon Elliott wrote:

Is there any way of only linking the stuff the user of the class needs,
and still keeping the pimpl in some shape or form?


Seperate the execBloggs(), and execJimmy() into seperate
classes. Have you class only see abstract bases. Clients
supply an implementation exec object contructed with an
appropriate object.

BloggsAbs.h
class BloggsExecAbs
{
public:
virtual void execBloggs() = 0;
};

bloggs_impl.h
class BloggsExecImpl : public BloggsAbs
{
public:
virtual void execBloggs();
};
foo.h
#include "BloggsAbs.h"
#include "JimmyAbs.h"

class foo
{
public:
void StartBloggs(BloggsExecAbs* bloggsExec) {
bloggsExec_ = bloggsStuff;
state_ = 1;
}
void StartJimmy(JimmyExecAbs* jimmyExec) {
jimmyExec_ = jimmyStuff;
state_ = 2;
}

void Exec(void) {
switch (state_) {
case 1:
bloggsExec_->execBloggs();
break;
case 2:
jimmyExec_->execJimmy();
break;
}
}
};

Jul 19 '05 #2
lilburne <li******@godzilla.net> writes
Seperate the execBloggs(), and execJimmy() into seperate
classes. Have you class only see abstract bases. Clients
supply an implementation exec object contructed with an
appropriate object.


Thanks - an interesting idea. I could even have all the relevant local
state data encapsulated into the implementation object.
--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #3

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

Similar topics

15
by: beliavsky | last post by:
What are the pros and cons of defining a method of a class versus defining a function that takes an instance of the class as an argument? In the example below, is it better to be able to write...
0
by: Simon Elliott | last post by:
I have a class which uses a singleton to output progress messages for logging or display. It's typically used deep within a hierarchy of aggregations, inheritances and containers. The singleton...
3
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
2
by: Rune Vistnes | last post by:
Hey, I am trying to wrap an unmanaged library in managed c++ so that I can use this library in other .NET languages, such as C#. I've been successful for the most part this far, but I'm having a...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
6
by: Milsnips | last post by:
Hi there, this is what i'm trying to achieve, i have separate identical classes for SqlClient, OracleClient, Odbc and OleDb, what i want is to create a Wrapper class that calls any of these 4...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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,...

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.