scalar(0) for a user defined type

  • Thread starter Thelma Roslyn Lubkin
  • Start date
T

Thelma Roslyn Lubkin

I use vector and matrix classes that I developed before they were
otherwise easily available. They are template classes that should be
able to use objects of other classes I've developed, e.g.
matrix<myclass<double> > amatrix(2,2)
declares a 2x2 matrix of objects of type myclass<double>.

My constructor for a matrix sets its elements to 0

for(int i=0; i<ilen; i++)
for(int j=0; j<jlen;j++)
mx[j] = scalar(0);

Is there any way for me to overload that 'scalar(0)' so that it's
defined for the myclass elements of amatrix?

Otherwise, how do I get around this? Do I need another constructor
that simply doesn't initialize the matrix?

This is my first attempt to write a matrix populated by templated
objects and I'm sure that this is only the first of many problems
I'll get entangled in.

thanks for any help,
--thelma
 
E

Erik Wikström

I use vector and matrix classes that I developed before they were
otherwise easily available. They are template classes that should be
able to use objects of other classes I've developed, e.g.
matrix<myclass<double> > amatrix(2,2)
declares a 2x2 matrix of objects of type myclass<double>.

My constructor for a matrix sets its elements to 0

for(int i=0; i<ilen; i++)
for(int j=0; j<jlen;j++)
mx[j] = scalar(0);

Is there any way for me to overload that 'scalar(0)' so that it's
defined for the myclass elements of amatrix?


If you have the template type something like this:

template<class T>
class matrix
{
// ...
};

Then you can use T() to get the default value for whatever T is, so
replace "mx[j] = scalar(0);" with "mx[j] = T();" in the loop and
make sure that myclass can be default-constructed and will set the
values of its member to something sensible (preferably by usint T() in
myclass too).
 
T

Thelma Roslyn Lubkin

: On 2007-11-18 15:07, Thelma Roslyn Lubkin wrote:
:> My constructor for a matrix sets its elements to 0
:>
:> for(int i=0; i<ilen; i++)
:> for(int j=0; j<jlen;j++)
:> mx[j] = scalar(0);
:>
:> Is there any way for me to overload that 'scalar(0)' so that it's
:> defined for the myclass elements of amatrix?

: If you have the template type something like this:

: template<class T>
: class matrix
: {
: // ...
: };

: Then you can use T() to get the default value for whatever T is, so
: replace "mx[j] = scalar(0);" with "mx[j] = T();" in the loop and
: make sure that myclass can be default-constructed and will set the
: values of its member to something sensible (preferably by usint T() in
: myclass too).

This means that I can't use the matrix(rowLen,colLen)
constructor if the matrix elements are such objects, because
mx[j] = T() wouldn't work for matrix elements that are
simple scalars like double -- so I'll need to provide a separate
constructor for elements that are objects?
--thelma
: --
: Erik Wikström
 
E

Erik Wikström

: On 2007-11-18 15:07, Thelma Roslyn Lubkin wrote:
:> My constructor for a matrix sets its elements to 0
:>
:> for(int i=0; i<ilen; i++)
:> for(int j=0; j<jlen;j++)
:> mx[j] = scalar(0);
:>
:> Is there any way for me to overload that 'scalar(0)' so that it's
:> defined for the myclass elements of amatrix?

: If you have the template type something like this:

: template<class T>
: class matrix
: {
: // ...
: };

: Then you can use T() to get the default value for whatever T is, so
: replace "mx[j] = scalar(0);" with "mx[j] = T();" in the loop and
: make sure that myclass can be default-constructed and will set the
: values of its member to something sensible (preferably by usint T() in
: myclass too).

This means that I can't use the matrix(rowLen,colLen)
constructor if the matrix elements are such objects, because
mx[j] = T() wouldn't work for matrix elements that are
simple scalars like double -- so I'll need to provide a separate
constructor for elements that are objects?


Actually, that is the beauty of it: it works for the built-in types too
and will set their value to 0 (I think for all of them) as demonstrated
by this simple code:

#include <iostream>

template<class T>
struct matrix
{
T t;
matrix()
{
t = T();
}
};

int main()
{
matrix<bool> m;
std::cout << m.t;
}
 
T

Thelma Roslyn Lubkin

: On 2007-11-18 16:57, Thelma Roslyn Lubkin wrote:
:> : On 2007-11-18 15:07, Thelma Roslyn Lubkin wrote:
:> :> Is there any way for me to overload that 'scalar(0)' so that it's
:> :> defined for the myclass elements of amatrix?
:>
:> : If you have the template type something like this:
:>
:> : template<class T>
:> : class matrix
:> : {
:> : // ...
:> : };
:>
:> : Then you can use T() to get the default value for whatever T is, so
:> : replace "mx[j] = scalar(0);" with "mx[j] = T();" <snip>

:> This means that I can't use the matrix(rowLen,colLen)
:> constructor if the matrix elements are such objects, because
:> mx[j] = T() wouldn't work for matrix elements that are
:> simple scalars like double -- so I'll need to provide a separate
:> constructor for elements that are objects?

: Actually, that is the beauty of it: it works for the built-in types too
: and will set their value to 0 (I think for all of them) as demonstrated
: by this simple code:

Thank you: that worked for me, for both integers and for my
objects. It truly is beautiful.
--thelma
: --
: Erik Wikström
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top