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

Proper way to put an abstract base class into a container

Suppose I have my own template container that represents a matrix. The
template's only parameter is < typename T > for the type that will go into
it. The only problem is that calling this matrix's constructor causes
issues with initiazing an abstract base type. What is the design pattern I
need to use to put this ABC into the container?
Jul 22 '05 #1
6 1462
"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote...
Suppose I have my own template container that represents a matrix. The
template's only parameter is < typename T > for the type that will go into
it. The only problem is that calling this matrix's constructor causes
issues with initiazing an abstract base type. What is the design pattern I need to use to put this ABC into the container?


You probably want to have a container of pointers to T.

Victor
Jul 22 '05 #2
"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cf**********@newsreader.wustl.edu...
Suppose I have my own template container that represents a matrix. The
template's only parameter is < typename T > for the type that will go into
it. The only problem is that calling this matrix's constructor causes
issues with initiazing an abstract base type. What is the design pattern I need to use to put this ABC into the container?


When people say that they have a class that represents a matrix, they are
usually writing a class that will perform number-crunching (matrix
multiplications, inverses, transposes, etc). Also, they usually will use it
to hold objects of type double or float (or possibly a light wrapper around
those types), and not instances of an ABC. An ABC might be an interesting
exercise, but for any application that made intensive use of the matrix, the
performance would probably be unacceptable. Why do you want to store an ABC
inside a matrix? As Victor pointed out, pointers are an option. They're
not all that easy to handle, though, if you want to write memory-leak-free
code.

--
David Hilsee
Jul 22 '05 #3
Aguilar, James wrote:
Suppose I have my own template container that represents a matrix. The
template's only parameter is < typename T > for the type that will go into
it. The only problem is that calling this matrix's constructor causes
issues with initiazing an abstract base type. What is the design pattern I
need to use to put this ABC into the container?


If you're using the STL, you're stuck with placing pointers in the
container. If you have a map or a list there may be alternatives e.g.

See this for one example using lists.
http://austria.sourceforge.net/dox/h...xibleList.html

You'll need to look at the test case to get an idea on how it works.

or even better -
http://www.elude.ca/aapl/

Jul 22 '05 #4

"David Hilsee" <da*************@yahoo.com> wrote in message
news:jP********************@comcast.com...

When people say that they have a class that represents a matrix, they are
usually writing a class that will perform number-crunching (matrix
multiplications, inverses, transposes, etc).
This is not a mathematical matrix, it is a matrix in the sense of a two
dimensional array (i.e. rows and columns, hence, matrix).
Also, they usually will use it
to hold objects of type double or float (or possibly a light wrapper around those types), and not instances of an ABC. An ABC might be an interesting
exercise, but for any application that made intensive use of the matrix, the performance would probably be unacceptable.
I wouldn't say that. It's just a one dimensional array of "<typename T>"s.
It is accessed through T operator ()(int, int) which just does array
arithmetic: return store_[rows * x + y]. Everything is inline, so it's
really no overhead at all, besides actually filling the matrix.
Why do you want to store an ABC
inside a matrix?
I'm doing a toy project which makes a 80x20 matrix of containers, then puts
particles in them (based on a text file). After that, it will iterate
through the matrix and randomly move each particle. The point is to
demonstrate that a gas, even if concentrated in one place initially, will
eventually diffuse into a larger area. This will be tried with all kinds of
shapes of containers to see how well the concept works and what kinds of
shapes keep gasses in the longest. Since a text file will dictate the shape
of the container, it should be no problem to reuse the programs on different
kinds of containers. Of course, the base class that I want to put into the
matrix is the bottom type (that is "Floor" or "Wall" at this point) which
will itself have a reference to a list of the particles on top of it.
As Victor pointed out, pointers are an option. They're
not all that easy to handle, though, if you want to write memory-leak-free
code.


That's one option. I'm not worried about memory leaks, since the program is
so small. I know exactly where everything is and in what order it happens.
Pointers . . . meh. I guess I will have to use one sometime, so it might as
well be now. I think I'll do that. Actually, I think I can still abstract
it and make it return references to the typename by making pointers
internally but dereferencing them outside. Well, I'll let y'all know how it
goes.

Yours,

James
Jul 22 '05 #5
"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cf**********@newsreader.wustl.edu...

"David Hilsee" <da*************@yahoo.com> wrote in message
news:jP********************@comcast.com...

When people say that they have a class that represents a matrix, they are usually writing a class that will perform number-crunching (matrix
multiplications, inverses, transposes, etc).
This is not a mathematical matrix, it is a matrix in the sense of a two
dimensional array (i.e. rows and columns, hence, matrix).
Also, they usually will use it
to hold objects of type double or float (or possibly a light wrapper

around
those types), and not instances of an ABC. An ABC might be an interesting exercise, but for any application that made intensive use of the matrix,

the
performance would probably be unacceptable.


I wouldn't say that. It's just a one dimensional array of "<typename

T>"s. It is accessed through T operator ()(int, int) which just does array
arithmetic: return store_[rows * x + y]. Everything is inline, so it's
really no overhead at all, besides actually filling the matrix.
In this case, I wouldn't worry about performance. I mentioned performance
because I thought that you might be doing some number crunching with the
"matrix" class, which you clearly aren't.
Why do you want to store an ABC
inside a matrix?


I'm doing a toy project which makes a 80x20 matrix of containers, then

puts particles in them (based on a text file). After that, it will iterate
through the matrix and randomly move each particle. The point is to
demonstrate that a gas, even if concentrated in one place initially, will
eventually diffuse into a larger area. This will be tried with all kinds of shapes of containers to see how well the concept works and what kinds of
shapes keep gasses in the longest. Since a text file will dictate the shape of the container, it should be no problem to reuse the programs on different kinds of containers. Of course, the base class that I want to put into the matrix is the bottom type (that is "Floor" or "Wall" at this point) which
will itself have a reference to a list of the particles on top of it.
As Victor pointed out, pointers are an option. They're
not all that easy to handle, though, if you want to write memory-leak-free code.
That's one option. I'm not worried about memory leaks, since the program

is so small. I know exactly where everything is and in what order it happens. Pointers . . . meh. I guess I will have to use one sometime, so it might as well be now. I think I'll do that. Actually, I think I can still abstract it and make it return references to the typename by making pointers
internally but dereferencing them outside. Well, I'll let y'all know how it goes.


I'm not a big physics or chemistry guy, but I would be interested in seeing
what the end result looks like.

--
David Hilsee
Jul 22 '05 #6
I will post a link to the source code when it is done, if I can find a place
to host the text files.
Jul 22 '05 #7

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

Similar topics

10
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
4
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
3
by: Daniel Billingsley | last post by:
The base class obviously can't know anything about a class which inherits from it, but is it acceptable to have a base class method which will be given information from an inherited class? For...
7
by: erin.sebastian | last post by:
Hello Everyone, This is my first attempt at coding using an abstract class and i am getting an error i can't figure out. Here is the back ground. I have a project that contains an abstract...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
8
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I have a couple of questions about the proper design of classes. I'll use a simple Customer class for my question. 1) Lets say that I have this Customer class like I said, and I want to...
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?
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,...
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
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...
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,...

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.