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. creating an array of objects dynamically

creating an array of objects dynamically

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestion
11 Posts 5 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.
  • E Offline
    E Offline
    erfi
    wrote on last edited by
    #1

    i have defined two class that you see below:

    class Row
    {
    private:
    char *termState;
    int *terms;
    public:
    Row(int length,int mTCount);

    };
    class Table
    {
    private:
    Row *rows;
    public:
    Table(int);
    }

    in Table() constructor i need to creat an array of Row objects dynamically like this :

    Table::Table(int n)
    {
    rows = new Row[n];
    }

    how i can call the Row() constructor for this array? is this possible? more information : in Row() constructor i want to allocate memory for (char *termState) and (int *terms) dynamically;

    T _ 2 Replies Last reply
    0
    • E erfi

      i have defined two class that you see below:

      class Row
      {
      private:
      char *termState;
      int *terms;
      public:
      Row(int length,int mTCount);

      };
      class Table
      {
      private:
      Row *rows;
      public:
      Table(int);
      }

      in Table() constructor i need to creat an array of Row objects dynamically like this :

      Table::Table(int n)
      {
      rows = new Row[n];
      }

      how i can call the Row() constructor for this array? is this possible? more information : in Row() constructor i want to allocate memory for (char *termState) and (int *terms) dynamically;

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      just a guess, why don't you use std::vector<> ?


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      E Z 2 Replies Last reply
      0
      • T toxcct

        just a guess, why don't you use std::vector<> ?


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        E Offline
        E Offline
        erfi
        wrote on last edited by
        #3

        i never used it. can you give me a link for this topic?

        T 1 Reply Last reply
        0
        • E erfi

          i never used it. can you give me a link for this topic?

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          MSDN : std::vector[^]

          std::vector<CMyType> vec;
          CMyType o1, o2;
          vec.push_back(o1);
          vec.push_back(o2);

          //to iterate through the vector :
          std::vector<CMyType>::iterator iter;
          for (iter = vec.begin(); iter != vec.end(); iter++) {
          CMyType obj = *iter;
          //...
          }


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          E 1 Reply Last reply
          0
          • T toxcct

            MSDN : std::vector[^]

            std::vector<CMyType> vec;
            CMyType o1, o2;
            vec.push_back(o1);
            vec.push_back(o2);

            //to iterate through the vector :
            std::vector<CMyType>::iterator iter;
            for (iter = vec.begin(); iter != vec.end(); iter++) {
            CMyType obj = *iter;
            //...
            }


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            E Offline
            E Offline
            erfi
            wrote on last edited by
            #5

            thanks toxcct I'll try it

            1 Reply Last reply
            0
            • E erfi

              i have defined two class that you see below:

              class Row
              {
              private:
              char *termState;
              int *terms;
              public:
              Row(int length,int mTCount);

              };
              class Table
              {
              private:
              Row *rows;
              public:
              Table(int);
              }

              in Table() constructor i need to creat an array of Row objects dynamically like this :

              Table::Table(int n)
              {
              rows = new Row[n];
              }

              how i can call the Row() constructor for this array? is this possible? more information : in Row() constructor i want to allocate memory for (char *termState) and (int *terms) dynamically;

              _ Offline
              _ Offline
              _AnsHUMAN_
              wrote on last edited by
              #6

              erfi wrote:

              how i can call the Row() constructor for this array? is this possible?

              It is possible to call the constructor of the row class. It get's invoked automatically on creation of an object. ex: This code is not optimized. You should be using destructor of the classes to free the total amount of memory being allocated in this process.

              #include "stdafx.h"
              #include 
              using namespace std;
              class Row
              {
              private:
              	char *termState;  
              	int *terms;    
              public:
              	Row(int length,int mTCount);
              	void Display(); 
              };
              Row::Row(int a,int b)
              {
              	cout<<"Constructor of the row class is invoked"<Display();
              	}
              }
              int _tmain(int argc, _TCHAR* argv[])
              {
              	Table t(10);
              	return 0;
              }
              

              Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

              E 1 Reply Last reply
              0
              • _ _AnsHUMAN_

                erfi wrote:

                how i can call the Row() constructor for this array? is this possible?

                It is possible to call the constructor of the row class. It get's invoked automatically on creation of an object. ex: This code is not optimized. You should be using destructor of the classes to free the total amount of memory being allocated in this process.

                #include "stdafx.h"
                #include 
                using namespace std;
                class Row
                {
                private:
                	char *termState;  
                	int *terms;    
                public:
                	Row(int length,int mTCount);
                	void Display(); 
                };
                Row::Row(int a,int b)
                {
                	cout<<"Constructor of the row class is invoked"<Display();
                	}
                }
                int _tmain(int argc, _TCHAR* argv[])
                {
                	Table t(10);
                	return 0;
                }
                

                Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                E Offline
                E Offline
                erfi
                wrote on last edited by
                #7

                thank you _AnShUmAn_ but in your code you write this :

                Row *rows[5];

                but i need a parametric length for rows, like this:

                Table::Table(int a)
                {
                rows = new Row[a];
                }

                when i compile this code this error appears : "error C2512: 'Row' : no appropriate default constructor available" i think that there is a better way: i want to delete the Row() constructor and after creating the Row objects dynamically , allocate the memory for Row object's member with a member function of Row. what do you think ? is this a good way?

                B _ 2 Replies Last reply
                0
                • E erfi

                  thank you _AnShUmAn_ but in your code you write this :

                  Row *rows[5];

                  but i need a parametric length for rows, like this:

                  Table::Table(int a)
                  {
                  rows = new Row[a];
                  }

                  when i compile this code this error appears : "error C2512: 'Row' : no appropriate default constructor available" i think that there is a better way: i want to delete the Row() constructor and after creating the Row objects dynamically , allocate the memory for Row object's member with a member function of Row. what do you think ? is this a good way?

                  B Offline
                  B Offline
                  benjymous
                  wrote on last edited by
                  #8

                  I agree with toxcc - using vectors for stuff like this would be much easier, as they're especially designed to work just like arrays, but to be resizable at runtime

                  -- Help me! I'm turning into a grapefruit! Buzzwords!

                  1 Reply Last reply
                  0
                  • E erfi

                    thank you _AnShUmAn_ but in your code you write this :

                    Row *rows[5];

                    but i need a parametric length for rows, like this:

                    Table::Table(int a)
                    {
                    rows = new Row[a];
                    }

                    when i compile this code this error appears : "error C2512: 'Row' : no appropriate default constructor available" i think that there is a better way: i want to delete the Row() constructor and after creating the Row objects dynamically , allocate the memory for Row object's member with a member function of Row. what do you think ? is this a good way?

                    _ Offline
                    _ Offline
                    _AnsHUMAN_
                    wrote on last edited by
                    #9

                    erfi wrote:

                    "error C2512: 'Row' : no appropriate default constructor available"

                    try providing a default constructor in your class.

                    erfi wrote:

                    allocate the memory for Row object's member with a member function of Row.

                    What's the problem with allocation of memory in the constructor itself. Here's the code for you:

                    #include "stdafx.h"
                    #include 
                    using namespace std;
                    class Row
                    {
                    private:
                    	char *termState;  
                    	int *terms;    
                    public:
                    	Row()
                    	{
                    		cout<<"In edefau"<Display();
                    }
                    int _tmain(int argc, _TCHAR* argv[])
                    {
                    	Table t(10);
                    	return 0;
                    }
                    

                    Try this out and let me know whether there exist some more problem.. -- modified at 9:42 Wednesday 20th September, 2006

                    Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                    T 1 Reply Last reply
                    0
                    • T toxcct

                      just a guess, why don't you use std::vector<> ?


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      Alternatively, he could use the boost/tr1 array template as well.

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      1 Reply Last reply
                      0
                      • _ _AnsHUMAN_

                        erfi wrote:

                        "error C2512: 'Row' : no appropriate default constructor available"

                        try providing a default constructor in your class.

                        erfi wrote:

                        allocate the memory for Row object's member with a member function of Row.

                        What's the problem with allocation of memory in the constructor itself. Here's the code for you:

                        #include "stdafx.h"
                        #include 
                        using namespace std;
                        class Row
                        {
                        private:
                        	char *termState;  
                        	int *terms;    
                        public:
                        	Row()
                        	{
                        		cout<<"In edefau"<Display();
                        }
                        int _tmain(int argc, _TCHAR* argv[])
                        {
                        	Table t(10);
                        	return 0;
                        }
                        

                        Try this out and let me know whether there exist some more problem.. -- modified at 9:42 Wednesday 20th September, 2006

                        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #11

                        dude, stop trying to reinvent the wheel. the STL provides all this already, in a better way... consider using vector ;P


                        TOXCCT >>> GEII power

                        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                        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