scalar(0) for a user defined type

Discussion in 'C++' started by Thelma Roslyn Lubkin, Nov 18, 2007.

  1. 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
     
    Thelma Roslyn Lubkin, Nov 18, 2007
    #1
    1. Advertising

  2. 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[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).

    --
    Erik Wikström
     
    Erik Wikström, Nov 18, 2007
    #2
    1. Advertising

  3. Erik Wikström <> 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[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
     
    Thelma Roslyn Lubkin, Nov 18, 2007
    #3
  4. On 2007-11-18 16:57, Thelma Roslyn Lubkin wrote:
    > Erik Wikström <> 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[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;
    }

    --
    Erik Wikström
     
    Erik Wikström, Nov 18, 2007
    #4
  5. Erik Wikström <> wrote:
    : On 2007-11-18 16:57, Thelma Roslyn Lubkin wrote:
    :> Erik Wikström <> 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
     
    Thelma Roslyn Lubkin, Nov 19, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Cavell
    Replies:
    2
    Views:
    1,503
    Rob Williscroft
    Mar 6, 2005
  2. Oodini
    Replies:
    1
    Views:
    1,822
    Keith Thompson
    Sep 27, 2005
  3. Replies:
    2
    Views:
    436
  4. Clint Olsen
    Replies:
    6
    Views:
    389
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    180
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page