473,509 Members | 10,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Designing a Volume Class

E G
Hi!

I am having problems in designing a class. First, I have a base class
that allocates a 3D data set and allows some other mathematical
operations with it, something like this:

template <typename T> class BasicArray
{
Jul 22 '05 #1
6 2123
In article <bu**********@news1.ucsd.edu>, E G wrote:
Hi!

I am having problems in designing a class. First, I have a base class
that allocates a 3D data set and allows some other mathematical
operations with it,
What mathematical operations do you need to do ? Think about this carefully,
it's important.
template <typename T> class BasicArray
{
.
.
.
private:
T ***Array;
Why use T*** and not a T* ? What are the advantages of this approach ? Have you
read the FAQ for this forum ?
public:
T operator()(unsigned,unsigned,unsigned);
Should be:

T& operator()(unsigned,unsigned,unsigned);
const T& operator() const(unsigned,unsigned,unsigned);
This class is not useful alone since I have to read the data from files.
The files may contain the information in an 3D array of any C basic type
(i.e., char, short, int, float, etcetera). This is the reason of
creating a template class. I decided to create a second class "Volume"
that handles the I/O and header information and uses BasicArray somehow.
Here's the problem: you don't know what data type is stored in the file at
compile time. So you need to handle this dynamically.

For example:

class FileReader;

class DoubleReader : public FileReader
{
....
};

....

Then the question is, do you always convert the data into a certain type of
BasicArray (BasicArray<double> or BasicArray<int> or even BasicArray<char>
depending on what you need to do with the data) or do you leave the data in
the same format that it's stored on disk ?

You need to think about how your classes are going to be used. Flexibility
may seem like a good thing, but gratuitous flexibility is often actually
just a nuisance. For example, having a "data set of any type" class could
result in worse performance, and create all sorts of nasty problems down
the road when you write your math ops.
Since the information of the basic type in which the information is
represented is stored in the file I cannot inherit Volume from
BasicArray.
Templates and inheritance don't mix that well. If you really did want to
handle this data polymorphically, you would need to derive BasicArray from
some other class:

class BaseArray
{
double operator() ( ..... );

....
};

class BasicArray<T> : public BaseArray
{
};

But then you're going to have one hell of a time doing things like
performing arithmatic ops on mixed types. For example, suppose you "add"
a BasicArray<double> to a BasicArray<int>. What type should the result
be ? You need a double dispatch. Also, if you are to make a variable return
type, it needs to be a polymorphic BaseArray* pointer, not a value.

But that's ugly. So now you need to wrap your BaseArray class in a handle.

You should read a book that discusses handle classes if you haven't yet done
so. Accelerated C++ would be a good start.

Have I convinced you that using an "any-datatype" class for general use is
probably not as good an idea as it may at first seem ?
I was thinking of creating the class Volume with a void *
pointer.


No! That's a horrible way to do it.

You would need

class ArrayHandle
{
BaseArray* impl;
public:
....

};

class BaseArray
{

....
};

template <typename T> class BasicArray : public BaseArray
{
...
};

Then you have to decide how the handle manages your array. Does it reference
count ? Does it deep copy ? Etc.

Then you need to think about how you handle mixed type arithmatic. For example,
does it make sense for the sum of two BasicArray<short> objects to be a
BasicArray<double> object ? You need to write "double dispatch" code which
in itself is a terrible mess.

Now the simplest solution would be to avoid the "any type" nonsense for
anything more complicated than element access and file reading. This way,
you only use "any type" for the limited and probablyt necessary task of
being able to make small changes to large files without changing the data
type of the whole file.

For mathematical operations, the appropriate model will usually be to convert
the on-disk data to an appropriate type. What an "appropriate type" is depends
on what is being done with the data. char is appropriate for a binary mask.
int is appropriate for discrete valued data, and for results of computations
which preserve discreteness (e.g. spatial masking) floating point types are
appropriate for storing the results of floating point computation on int data.

The person using the code for a particular application is the right person
to decide what type of Array is the appropriate one to use.

Sometimes, there may not be a sensible conversion (for example, double->char).
In this case, there should be some way to throw an exception or something,
to tell the programmer using the class that the file open "failed" because
the file was "unsuitable" for their intended use. Again, this must be handled
at runtime since you don't know what will be in the file at compile time.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #2
E G wrote:
Hi!

I am having problems in designing a class. First, I have a base class
that allocates a 3D data set and allows some other mathematical
operations with it, something like this:

template <typename T> class BasicArray
{
.
.
.
private:
T ***Array;
.
.
.
public:
T operator()(unsigned,unsigned,unsigned);
BasicArray<T> operator+(T op);
.
.
.
}

This class is not useful alone since I have to read the data from files.
The files may contain the information in an 3D array of any C basic type
(i.e., char, short, int, float, etcetera). This is the reason of
creating a template class. I decided to create a second class "Volume"
that handles the I/O and header information and uses BasicArray somehow.
Since the information of the basic type in which the information is
represented is stored in the file I cannot inherit Volume from
BasicArray. I was thinking of creating the class Volume with a void *
pointer. This pointer would point to the appropiate instance of the
BasicArray<type>. However, I am not sure that this is the best approach.
Any ideas are greatly appreciated.

Regards!


containment.

template <typename T>
class Volume {
private:
BasicArray<T> data;
public:
BasicArray<T>& get_array();
const BasicArray<T>& get_array() const;
// yada yada yada
};
Jul 22 '05 #3
In article <4_*****************@newssvr29.news.prodigy.com> , red floyd wrote:
E G wrote: containment.

template <typename T>
class Volume {
private:
BasicArray<T> data;
public:
BasicArray<T>& get_array();
const BasicArray<T>& get_array() const;
// yada yada yada
};


And how does this help ?

--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #4
Donovan Rebbechi wrote:
In article <4_*****************@newssvr29.news.prodigy.com> , red floyd wrote:
E G wrote:

containment.

template <typename T>
class Volume {
private:
BasicArray<T> data;
public:
BasicArray<T>& get_array();
const BasicArray<T>& get_array() const;
// yada yada yada
};

And how does this help ?


He wanted to know how he could use BasicArray<> with Volume, but said
that he had problems with inheritance. It seems to me that containment
is a much better solution.

DISCLAIMER: I may have misread the OP.
Jul 22 '05 #5
In article <q2*****************@newssvr29.news.prodigy.com> , red floyd wrote:
Donovan Rebbechi wrote:
In article <4_*****************@newssvr29.news.prodigy.com> , red floyd wrote:
E G wrote:

containment.

template <typename T>
class Volume {
private:
BasicArray<T> data;
public:
BasicArray<T>& get_array();
const BasicArray<T>& get_array() const;
// yada yada yada
};

And how does this help ?


He wanted to know how he could use BasicArray<> with Volume, but said
that he had problems with inheritance. It seems to me that containment
is a much better solution.


Your example is not a solution because it doesn't actually solve anything.

The problems he was dealing with were associated with handling data (read via
input files) whose type is not known at compile time. Your volume class doesn't
address this (because your class still has a template parameter which must be
presented at compile time), and in fact it, as you've presented it, your
code doesn't really solve anything else either.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #6

"E G" <eg******@ucsd.edu> wrote in message
news:bu**********@news1.ucsd.edu...
Hi!

I am having problems in designing a class. First, I have a base class
that allocates a 3D data set and allows some other mathematical
operations with it, something like this:

template <typename T> class BasicArray
{
.
.
.
private:
T ***Array;
.
.
.
public:
T operator()(unsigned,unsigned,unsigned);
BasicArray<T> operator+(T op);
.
.
.
}

This class is not useful alone since I have to read the data from files.
The files may contain the information in an 3D array of any C basic type
(i.e., char, short, int, float, etcetera). This is the reason of
creating a template class. I decided to create a second class "Volume"
that handles the I/O and header information and uses BasicArray somehow.
Since the information of the basic type in which the information is
represented is stored in the file I cannot inherit Volume from
BasicArray. I was thinking of creating the class Volume with a void *
pointer. This pointer would point to the appropiate instance of the
BasicArray<type>. However, I am not sure that this is the best approach.
Any ideas are greatly appreciated.

Regards!


What you've got there looks good already. A custom array class specifically
designed to suit your project.
Stick some logic in your volume class i.e:
if(type=="int")
BasicArray<int> name;
else if(type=="char")
BasicArray<char> name;
etc etc.

Then your array is initialised with the correct type. What you might want to
do is keep then seperate. One class for the business logic and one for the
data. So design the array class so the volume class can communicate with the
it easily.
For example say your volume class had a function named getfiledata(8 bytes)
then you make that function not only get the data but also fill the array
with the data. Thus you need a good interface so make your array classes
accessor methods suitable.

The array class doens't need to be inherintely related to I don't think but
I don't know enough about your project.
You could always nest the array class inside the volume class too remember.



Jul 22 '05 #7

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

Similar topics

12
1869
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
2
1398
by: Sky Sigal | last post by:
Hello: I'm currently messing around, and need as much feedback/help as I can get, trying to find the most economical/graceful way to build usercontrols that rely on styling to look any good... ...
2
14413
by: Bob Day | last post by:
Using VS 2003, VB. Net, MSDE... Usining task sheduler, I wish to mute the volume in the .bat file that task scheduler runs (windows XP Pro). I don't see anyway to do this via a .bat line...
3
6710
by: Andreas Hecker | last post by:
Hello, when i play a sound file like mp3 or wav with MediaPlayer or anything like this, i would like to get the volume level of the sound card in real time. The data is then sent to the serial...
16
6085
by: Basil Fawlty | last post by:
Hi everyone, I have an assignment, to create a simple VB program that computes the volume of a cylinder. The Form is pretty simple, it has a label and text box for base radius, another for height...
1
2611
by: Christopher A. Kelly | last post by:
I need to figure out how to control any 3rd party application by simulating mouse click and key presses. I need to take a partial window title and get the handle then be able to send the necessary...
7
7631
by: Rich Milburn [MVP] | last post by:
Ok I am not a programmer, I copied some code and very painfully got it working in VB6. I can adjust the volume with waveOutSetVolume on winmm.dll. But I could not seem to be able to figure out how...
1
3185
by: arozow | last post by:
I want to control the volume. But it's not working. <object id="dmo" name="dmo" class="dmo" hidden="true" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="0" height="0" > ...
1
10058
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
0
7237
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
7137
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
7347
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
7416
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...
1
7073
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5062
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...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.