472,794 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Design discussion: inheritance + (template or parameters)

Hi,

I am trying to implement a class hierarchy where there will be a base class
defining the interface and some common operations (interface+implentation):
Constraints : no STL. This will be implemented for an ARM processor for an
embedded application.

class ActorA: public Actor;
class ActorB: public Actor;
class ActorC: public Actor;

Actor {
virtual void decide() = 0;
void Preprocess();
virtual void Process() = 0;
}

Decide() and Process() are interfaces defined by this base class.

My problem is with the types associated with these actor objects. Internally
all these classes act on a two dimensional array of values of size (X, Y)
which will be configurable. SizeX and SizeY are in the order of 20-40. Each
element of the array can be a single value, two values, or 7 values. .
Specifically,

ActorA will act on a 2-D array of single values
ActorB will act on a 2-D array of doublets.
ActorC will act on a 2-D array of 7-lets.

The fact that you may have 1, 2, or 7 values only affect the decide()
function. The process() functions acts on each of these values independently
of the decision is made.

Possibilities:
1) Everything is a parameter
Actor::Actor(SizeX, SizeY, N) {
// N : n-tuple
pArray = new int(SizeX*SizeY*N);
// Define access operators
// etc.

Actor will be general enough to do everything.
ActorX will be using the access operators etc. from Actor class and just
implement decide() and process().

2) Define a template
template <int SizeX, int SizeY, int N>
class Actor {
private:
int[SizeX][SizeY][N];
public:
Actor() {}
// etc.

Alll other actor classes will be inherited from this template base class. i
believe this is doable. No need to define access operators in this case.

Those are the two ideas I have. Later there may be a need to define other
Actors defining other decide/process pairs. As you realize, SizeX and Y are
not known at compile time, but N is known. I would be happy to find a way to
make the dependency on N specific to actors. Template based idea seems to do
it, but I am not sure...

Which one is better? Which is a better design? code size is a slightly
lesser issue than speed.

Thanks for the feedback.

Arcin

Jul 19 '05 #1
2 2086
arch wrote:
....
The fact that you may have 1, 2, or 7 values only affect the decide()
function. The process() functions acts on each of these values independently
of the decision is made.

Possibilities:
1) Everything is a parameter
Actor::Actor(SizeX, SizeY, N) {
// N : n-tuple
pArray = new int(SizeX*SizeY*N);
// Define access operators
// etc.

Actor will be general enough to do everything.
ActorX will be using the access operators etc. from Actor class and just
implement decide() and process().

2) Define a template
template <int SizeX, int SizeY, int N>
class Actor {
private:
int[SizeX][SizeY][N];
public:
Actor() {}
// etc.

Alll other actor classes will be inherited from this template base class. i
believe this is doable. No need to define access operators in this case.

Those are the two ideas I have. Later there may be a need to define other
Actors defining other decide/process pairs. As you realize, SizeX and Y are
not known at compile time, but N is known.
If SizeX and SizeY are not known at compile time then the preceeding
template won't work for you. Template parameter values MUST BE KNOWN at
compile time.

I would be happy to find a way to make the dependency on N specific to actors. Template based idea seems to do
it, but I am not sure...

Which one is better? Which is a better design? code size is a slightly
lesser issue than speed.


Another alternative:

template <int N>
class Actor {
private:
typedef int value_type[N];
value_type * m_values;
int m_xsize;
int m_ysize;

public:
Actor(int SizeX, int SizeY)
: values( new value_type[ SizeX * SizeY ] ),
m_xsize( SizeX ),
m_ysize( SizeY )
{
}
};
I really don't have enough to go on to help you but you may get a slight
performance improvement by using the Actor<int> template because there
is a constant multiply and hence the compliler may be able to produce
some multiply optimizations.

Jul 19 '05 #2

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bh********@dispatch.concentric.net...
arch wrote:
...
The fact that you may have 1, 2, or 7 values only affect the decide()
function. The process() functions acts on each of these values independently of the decision is made.

Possibilities:
1) Everything is a parameter
Actor::Actor(SizeX, SizeY, N) {
// N : n-tuple
pArray = new int(SizeX*SizeY*N);
// Define access operators
// etc.

Actor will be general enough to do everything.
ActorX will be using the access operators etc. from Actor class and just
implement decide() and process().

2) Define a template
template <int SizeX, int SizeY, int N>
class Actor {
private:
int[SizeX][SizeY][N];
public:
Actor() {}
// etc.

Alll other actor classes will be inherited from this template base class. i believe this is doable. No need to define access operators in this case.

Those are the two ideas I have. Later there may be a need to define other Actors defining other decide/process pairs. As you realize, SizeX and Y are not known at compile time, but N is known.
If SizeX and SizeY are not known at compile time then the preceeding
template won't work for you. Template parameter values MUST BE KNOWN at
compile time.


I thought I would be able to do it. Oh, what you are saying is that if I
have
SizeX = GetInput();
SizeY=GetInput();
Actor<SizeX, SizeY, 7> a; // this will not work because SizeX and SizeY are
not constants.
I would be happy to find a way to
make the dependency on N specific to actors. Template based idea seems to do it, but I am not sure...

Which one is better? Which is a better design? code size is a slightly
lesser issue than speed.
Another alternative:

template <int N>
class Actor {
private:
typedef int value_type[N];
value_type * m_values;
int m_xsize;
int m_ysize;

public:
Actor(int SizeX, int SizeY)
: values( new value_type[ SizeX * SizeY ] ),
m_xsize( SizeX ),
m_ysize( SizeY )
{
}
};


I like your alternative in the sense that int[N] typedef makes it look like
an internal type. And since I know the value of N for different actors, that
works great. Since template is out of the picture, that is sth between a
full template (which is not possible) and full 1-D array implementation.

And this fits the objects in my domain a lot closer.

Thank you.

I am wondering: Is it worth defining classes for these different int[N]
datatypes. OR Let's assume that these classes are created for other needs.
In this case the template will turn into

template <typename NewType>
class Actor {
private:
NewType *mp_values;
//etc...
}

Each NewType will include the appropriate int[N] private member where N is
1,2,7, etc.

it is not really look useful to create them unless they are created for
other purposes. To me, even when these NewTypes are created, it may be
simpler (=faster?) to stick to your definition of Actor.

I really don't have enough to go on to help you but you may get a slight
performance improvement by using the Actor<int> template because there
is a constant multiply and hence the compliler may be able to produce
some multiply optimizations.


Jul 19 '05 #3

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

Similar topics

14
by: | last post by:
Hi, I was performing SQL UPDATE queries and I notice that they SUCCEED on the ExecuteNonQuery() call with NO exceptions raised BUT they fail at the Database. They say they succeed in the code...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
11
by: | last post by:
I have a rather newbie question regarding design of class hierarchy. Suppose I have a class Class0, and need to implement a public Class0.compute() interface. There are three different ways to...
3
by: Gandu | last post by:
Could some C++ guru please help me. I have a template based general linked list class that I want to inherit publicly to create a queue class. In the case of non-template inheritance, I can use:...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.