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

Inheritance, templates, nothing solves my problem!?

Hi all,

I looked in several books, articles, etc. but did not find a solution
to my problem. Maybe somebody out there can help a desperate, not toooo
experienced programmer:

I want to represent functional blocks by lists of parameters.
Lets say, functional blocks represent different algorithms which, of
course, have very different parameters.
Thus, a functional block contains lists of parameters which, and now
comes the problem, can be of different types. It is not known at
compile time, how many parameters and which types.
The parameters are characterized by their name.

A parameter should be an object like this

set/getValue()
set/get/MaxMin()
string set/getName()

The value can be of type int or double (and maybe, who knows, in the
future there will be other types).
Thus, it is not possible to do it via an abstract base class
abstractParam and inheritance since the inherited classes would differ
in type (and which type should I supply to the abstract base class).

Sounds like templates would be a solution but, then, how should I
maintain a list of parameters in a functional block?

Parameter:
template <class Tparam:

FunctionalBlock ... {
list<paramparams;
....
}

does not work since the list expects specialized values, thus, params
must carry type information.

I could just put two lists in a functional block, one with int
parameters and one with double parameters.
Disadvantages: I have to iterate over two lists which is not that bad,
but if in the future a third type will be added, I have to modify all
the code dealing with this.

Any ideas for a solution?

Thanks in advance,

Rainer

Dec 5 '06 #1
4 1195

RT*****@web.de napsal:
Hi all,

I looked in several books, articles, etc. but did not find a solution
to my problem. Maybe somebody out there can help a desperate, not toooo
experienced programmer:

I want to represent functional blocks by lists of parameters.
Lets say, functional blocks represent different algorithms which, of
course, have very different parameters.
Thus, a functional block contains lists of parameters which, and now
comes the problem, can be of different types. It is not known at
compile time, how many parameters and which types.
The parameters are characterized by their name.

A parameter should be an object like this

set/getValue()
set/get/MaxMin()
string set/getName()

The value can be of type int or double (and maybe, who knows, in the
future there will be other types).
Thus, it is not possible to do it via an abstract base class
abstractParam and inheritance since the inherited classes would differ
in type (and which type should I supply to the abstract base class).

Sounds like templates would be a solution but, then, how should I
maintain a list of parameters in a functional block?

Parameter:
template <class Tparam:

FunctionalBlock ... {
list<paramparams;
...
}

does not work since the list expects specialized values, thus, params
must carry type information.

I could just put two lists in a functional block, one with int
parameters and one with double parameters.
Disadvantages: I have to iterate over two lists which is not that bad,
but if in the future a third type will be added, I have to modify all
the code dealing with this.

Any ideas for a solution?

Thanks in advance,

Rainer
Hi Rainer.

I would store one parameter in some variant class (let's say
Parameter), which knows the type of parameter (int, double, etc.). If
you define for such class copy constructor and assignment operator, you
can easily store instances of Parameter in std::map<std::string,
Parameter>. values in this map are indexed with std::string, so it is
name of parameter and there is simple to find them.

Ondra

Dec 5 '06 #2
Ondra Holub schrieb:
RT*****@web.de napsal:
I want to represent functional blocks by lists of parameters.
Lets say, functional blocks represent different algorithms which, of
course, have very different parameters.
Thus, a functional block contains lists of parameters which, and now
comes the problem, can be of different types. It is not known at
compile time, how many parameters and which types.
The parameters are characterized by their name.

A parameter should be an object like this

set/getValue()
set/get/MaxMin()
string set/getName()

The value can be of type int or double (and maybe, who knows, in the
future there will be other types).
Thus, it is not possible to do it via an abstract base class
abstractParam and inheritance since the inherited classes would differ
in type (and which type should I supply to the abstract base class).

Sounds like templates would be a solution but, then, how should I
maintain a list of parameters in a functional block?

Parameter:
template <class Tparam:

FunctionalBlock ... {
list<paramparams;
...
}

does not work since the list expects specialized values, thus, params
must carry type information.

I could just put two lists in a functional block, one with int
parameters and one with double parameters.
Disadvantages: I have to iterate over two lists which is not that bad,
but if in the future a third type will be added, I have to modify all
the code dealing with this.

Hi Rainer.

I would store one parameter in some variant class (let's say
Parameter), which knows the type of parameter (int, double, etc.). If
you define for such class copy constructor and assignment operator, you
can easily store instances of Parameter in std::map<std::string,
Parameter>. values in this map are indexed with std::string, so it is
name of parameter and there is simple to find them.
Hi Ondra,

never heard of a variant class, so after googling for it, I was
referred to the boos library and it seems, this library provides what I
need.

Another approach would be to construct only one type holding a double
and cast it down to int if needed. The strings are anyway read from a
table so this could also be done by doubles casted to int.
I wonder, how costly this boost::variant is. Do you have an idea?

--
Regards,

Rainer

Dec 5 '06 #3
Hi Ondra,
>
never heard of a variant class, so after googling for it, I was
referred to the boos library and it seems, this library provides what I
need.

Another approach would be to construct only one type holding a double
and cast it down to int if needed. The strings are anyway read from a
table so this could also be done by doubles casted to int.
I wonder, how costly this boost::variant is. Do you have an idea?

--
Regards,

Rainer
Hi Rainer.

I have never used boost::variant, but all boost classes are written
very well. Most of them will be part of next C++ standard.

With variant class I meant some class, which is able to store more than
1 type. It is not problem, there may be stored as many attributes as
memory allows. There must be also some mechanism how to check, which
type is currently stored inside. Something like this:

class Parameter
{
public:
typedef
enum { INT, DOUBLE } Type;

Parameter(int data)
: type_(INT),
idata_(data)
{
}

Parameter(double data)
: type_(DOUBLE),
ddata_(data)
{
}

Parameter(const Parameter& src)
: type_(src.type_),
idata_(src.idata_),
ddata_(src.ddata_)
{
}

Parameter& operator=(const Parameter& src)
{
type_ = src.type_;
idata_ = src.idata_;
ddata_ = src.ddata_;

return *this;
}

Type GetType() const { return type_; }

void SetData(int data)
{
type_ = INT;
idata_ = data;
}

void SetData(double data)
{
type_ = DOUBLE;
ddata_ = data;
}

int GetIntData() const { assert(type_ == INT); return idata_; }
double GetDoubleData() const { assert(type_ == DOUBLE); return
ddata_; }

private:
Type type_;
int idata_;
double ddata_;
};

Solution in boost is more generic, my solution is simpler but harder to
maintain in case of need to add more types.

Ondra

Dec 5 '06 #4
Ondra Holub wrote:
I have never used boost::variant, but all boost classes are written
very well. Most of them will be part of next C++ standard.
No, a *few* of them will *likely* be part of the next C++ standard
library.

Cheers! --M

Dec 5 '06 #5

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

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a...
15
by: scorpion53061 | last post by:
I am just now really starting to get into inheritance - controls, datasets, views all that stuff and I am seeing the enormous savings in performance and even watching the exe file size go down...
9
by: KraftDiner | last post by:
I have a class class MyClass(MyBaseClass) def __init__(self) super(self.__class__, self).__init__() self.type = MyClassType return self It has a few methods... I have another class and the...
4
by: Joe | last post by:
We are working on a project that requires 3rd parties (ie, consultants) to use our ASP.net webforms in a very reusable manner. The big catch is that we are not allowed to give them source. There...
2
by: tirzanello | last post by:
Hi guys, I'm becoming crazy with templates, maybe for it's a trivial problem... but I can't go through :-( any hint will be appreciated. I show you without templates a piece of code: 2 classes,...
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?

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.