Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > scalar(0) for a user defined type

Reply
Thread Tools

scalar(0) for a user defined type

 
 
Thelma Roslyn Lubkin
Guest
Posts: n/a
 
      11-18-2007
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[i][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
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      11-18-2007
On 2007-11-18 15:07, Thelma Roslyn Lubkin wrote:
> 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[i][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[i][j] = scalar(0);" with "mx[i][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).

--
Erik Wikström
 
Reply With Quote
 
 
 
 
Thelma Roslyn Lubkin
Guest
Posts: n/a
 
      11-18-2007
Erik Wikström <Erik-> wrote:
: 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[i][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[i][j] = scalar(0);" with "mx[i][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[i][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
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      11-18-2007
On 2007-11-18 16:57, Thelma Roslyn Lubkin wrote:
> Erik Wikström <Erik-> wrote:
> : 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[i][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[i][j] = scalar(0);" with "mx[i][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[i][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;
}

--
Erik Wikström
 
Reply With Quote
 
Thelma Roslyn Lubkin
Guest
Posts: n/a
 
      11-19-2007
Erik Wikström <Erik-> wrote:
: On 2007-11-18 16:57, Thelma Roslyn Lubkin wrote:
:> Erik Wikström <Erik-> 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[i][j] = scalar(0);" with "mx[i][j] = T();" <snip>

:> This means that I can't use the matrix(rowLen,colLen)
:> constructor if the matrix elements are such objects, because
:> mx[i][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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mapping betwing a standard type and a user defined type with SWIG Lyes Amazouz Ruby 2 08-20-2008 04:01 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
Type conversion function for user defined type... zaeminkr@gmail.com C++ 1 05-16-2007 09:00 AM
casting primitive type to user-defined type works in usage xllx.relient.xllx@gmail.com C++ 2 04-15-2006 05:37 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57