CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Blitz Library Strange Initialization

    I have been trying to figure out how the Blitz numeric library was able to get commas to initialize Vectors and Matrices. For example,

    Code:
    TinyMatrix<double, 2, 2> m;
    m = 1, 2, 3, 4;
    is perfectly valid. I have looked through the source, but I don't see any operator,(T_numtype n) in there. What did he do?

  2. #2
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: Blitz Library Strange Initialization

    I figured it out! What it does is in the operator= overload for TinyMatrix it returns an object that has operator,(T_numtype) overloaded. That object takes as an argument an iterator to TinyMatrix. The operator, simply increments the iterator each time a new number is encountered, pretty sneaky. I made an ultra simple example utilizing the same trick (without iterators) if anyone here is curious.

    Code:
    class ListInit
    {
     int* parr;
    public:
     ListInit() { parr = NULL; }
     ListInit(int* parrz) { parr = parrz; }
     ListInit& operator,(int num) {
      *parr = num;
      parr++;
      return *this;
     }
    };
    
    class Array
    {
     int arr[3];
    public:
     Array() 
     {
      arr[0] = 0;
      arr[1] = 0;
      arr[2] = 0;
     }
     
     Array(int x1, int x2, int x3) 
     {
      arr[0] = x1;
      arr[1] = x2;
      arr[2] = x3;
     }
     
     ListInit operator=(int x) 
     {
      arr[0] = x;
      return ListInit(&arr[1]);
     }
     
     void Print() 
     {
      cout << arr[0] << ", " << arr[1] << ", " << arr[2] << endl;
     }
    };
    
    int main(int argc, char *argv[])
    {
     Array x;
     x = 1, 2, 3;
     x.Print();
     return EXIT_SUCCESS;
    }
    Boost has a library completely dedicated to this kind of assignment. It is called "assign." It seems pretty interesting. Thanks for at least looking.

  3. #3
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Blitz Library Strange Initialization

    thats interesting... wuld be good for sizing multidimensionals.
    but there is this design issue here what would be best to use?
    maybe Array.Size = 1, 2, 3; or Array.Size = [1][2][3]; (if it wuld be possible) or Array.Size[1][2][3] or ()'s

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Blitz Library Strange Initialization

    Something like Array.SetSize[1][2][3] should be possible.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  5. #5
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Blitz Library Strange Initialization

    yes but at design what would be better? Array.Size = 1, 2, 3; or Array.Size[1][2][3];

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured