Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Assign a list of values to a class

Assign a list of values to a class

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anton A Loukine
    wrote on last edited by
    #1

    Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks

    J V B 3 Replies Last reply
    0
    • A Anton A Loukine

      Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks

      J Offline
      J Offline
      Jon Hulatt
      wrote on last edited by
      #2

      of course it's possible!

      int a[2][2] = { {1,2} , {3,4} };

      #include <beer.h>

      A 1 Reply Last reply
      0
      • J Jon Hulatt

        of course it's possible!

        int a[2][2] = { {1,2} , {3,4} };

        #include <beer.h>

        A Offline
        A Offline
        Anton A Loukine
        wrote on last edited by
        #3

        The thing is that I am assigning this array to a class, which has variables other than the 2-dimensional array or numbers (I don't think shallow assignment will do here): class matrix { private: double **_m; double *_v; int _nType, _nRows, _nCols; void _Copy( const matrix &m1 ); void _Destroy( ); public: // Default constructor matrix( int nMatrixType = mxMatrix, int nDim1 = 0, int nDim2 = 0 ); // Copy constructor matrix( const matrix &m1 ); ~matrix( ); matrix& operator = ( const matrix &m1 ); matrix operator * ( const matrix &m1 ); int GetType( ) { return _nType; } int GetRows( ) { return _nRows; } int GetCols( ) { return _nCols; } void Display( ) const; double GetElement( int nDim1, int nDim2 = 0 ) const; void SetElement( double fValue, int nDim1, int nDim2 = 0 ); matrix Transpose( ); matrix Solve( const matrix &mRHS ); };

        1 Reply Last reply
        0
        • A Anton A Loukine

          Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks

          V Offline
          V Offline
          valikac
          wrote on last edited by
          #4

          Does your design require dynamic allocation? In other words, does the program dynamically allocate matrices during run-time? If not, one solution is an STL container such as a list of paired values. Kuphryn

          A 1 Reply Last reply
          0
          • V valikac

            Does your design require dynamic allocation? In other words, does the program dynamically allocate matrices during run-time? If not, one solution is an STL container such as a list of paired values. Kuphryn

            A Offline
            A Offline
            Anton A Loukine
            wrote on last edited by
            #5

            The program DOES allocate matrices dynamically; however, there is going to be a fixed 9 x 9 matrix with predetermined values that is going to be used in calculations (this matrix needs to be of the same type (class) as the other dynamically allocated matrices). I just want to avoid typing the following 81 times: m.SetElement(0, 0, 100.0); m.SetElement(0, 0, 200.0); ..... and replace it with something like m = { 100.0, 200.0, ... } Thanks.

            V 1 Reply Last reply
            0
            • A Anton A Loukine

              The program DOES allocate matrices dynamically; however, there is going to be a fixed 9 x 9 matrix with predetermined values that is going to be used in calculations (this matrix needs to be of the same type (class) as the other dynamically allocated matrices). I just want to avoid typing the following 81 times: m.SetElement(0, 0, 100.0); m.SetElement(0, 0, 200.0); ..... and replace it with something like m = { 100.0, 200.0, ... } Thanks.

              V Offline
              V Offline
              valikac
              wrote on last edited by
              #6

              One solution for predefined values is a loop. Kuphryn

              1 Reply Last reply
              0
              • A Anton A Loukine

                Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks

                B Offline
                B Offline
                Blade DMS
                wrote on last edited by
                #7

                How about using varargs..? define a member function in the class which loads the values from a vararg list... you loose type checking, and will have to indicate the number of arguments somehow... Blade[DMS]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups