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. operator new

operator new

Scheduled Pinned Locked Moved C / C++ / MFC
questionphpcomdata-structuresregex
8 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.
  • S Offline
    S Offline
    Sarath C
    wrote on last edited by
    #1

    See the sample code Example:

    class MyClass
    {
    MyClass(int); // parameterized ctor
    };

    int main()
    {
    MyClass* p = new MyClass(10); // ok fine!
    MyClass* p1 = new MyClass[6];// error no default ctor available.
    return 0;
    }

    how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?

    SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."


    My Blog | Understanding State Pattern

    S A Z 3 Replies Last reply
    0
    • S Sarath C

      See the sample code Example:

      class MyClass
      {
      MyClass(int); // parameterized ctor
      };

      int main()
      {
      MyClass* p = new MyClass(10); // ok fine!
      MyClass* p1 = new MyClass[6];// error no default ctor available.
      return 0;
      }

      how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?

      SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."


      My Blog | Understanding State Pattern

      S Offline
      S Offline
      Steen Krogsgaard
      wrote on last edited by
      #2

      You have to supply a default constructor. From MSDN: "No explicit per-element initialization can be done when allocating arrays using the new operator; only the default constructor, if present, is called. See Default Arguments for more information" (MSDN[^])

      Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

      S 1 Reply Last reply
      0
      • S Sarath C

        See the sample code Example:

        class MyClass
        {
        MyClass(int); // parameterized ctor
        };

        int main()
        {
        MyClass* p = new MyClass(10); // ok fine!
        MyClass* p1 = new MyClass[6];// error no default ctor available.
        return 0;
        }

        how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?

        SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."


        My Blog | Understanding State Pattern

        A Offline
        A Offline
        AbhishekBK
        wrote on last edited by
        #3

        This is what I just wrote on VC7.0. #include class MyClass { public: MyClass(int a){} MyClass(){} }; int main() { MyClass* p1 = new MyClass(6); MyClass* p2 = new MyClass[6]; } This works just fine. -- modified at 6:50 Tuesday 15th August, 2006

        Abhishek

        1 Reply Last reply
        0
        • S Steen Krogsgaard

          You have to supply a default constructor. From MSDN: "No explicit per-element initialization can be done when allocating arrays using the new operator; only the default constructor, if present, is called. See Default Arguments for more information" (MSDN[^])

          Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          so we should have some "Set" function to do the task of parameterized constructor right?

          SaRath.
          _"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"


          My Blog | Understanding State Pattern_

          S 1 Reply Last reply
          0
          • S Sarath C

            so we should have some "Set" function to do the task of parameterized constructor right?

            SaRath.
            _"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"


            My Blog | Understanding State Pattern_

            S Offline
            S Offline
            Steen Krogsgaard
            wrote on last edited by
            #5

            Right. If you wan't to be politically OOP correct you write accessor functions to your member variables. In this case, with the member being an int, I would just assign directly to it in a loop.

            Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

            S 1 Reply Last reply
            0
            • S Steen Krogsgaard

              Right. If you wan't to be politically OOP correct you write accessor functions to your member variables. In this case, with the member being an int, I would just assign directly to it in a loop.

              Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

              S Offline
              S Offline
              Sarath C
              wrote on last edited by
              #6

              it would be helpful if you say how such a operation is restricted with arrays. but it can be used with single object. is it the difficulty for compiler in writing same code for more than one object? or any oothr purposeful use for that?

              SaRath.
              _"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"


              My Blog | Understanding State Pattern_

              S 1 Reply Last reply
              0
              • S Sarath C

                it would be helpful if you say how such a operation is restricted with arrays. but it can be used with single object. is it the difficulty for compiler in writing same code for more than one object? or any oothr purposeful use for that?

                SaRath.
                _"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"


                My Blog | Understanding State Pattern_

                S Offline
                S Offline
                Steen Krogsgaard
                wrote on last edited by
                #7

                Honestly, I have no idea why the compiler doesn't support initializing arrays of classes during construction, but according to MSDN that's just the way it is. Perhaps there's some sort of breach of the standard or a problem with the syntax. I don't know. I don't know what's your use of this is, but maybe it will be easier for you to use an array of pointers instead of a pointer to an array. Then you can allocate and initialize each element in a loop instead.

                Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

                1 Reply Last reply
                0
                • S Sarath C

                  See the sample code Example:

                  class MyClass
                  {
                  MyClass(int); // parameterized ctor
                  };

                  int main()
                  {
                  MyClass* p = new MyClass(10); // ok fine!
                  MyClass* p1 = new MyClass[6];// error no default ctor available.
                  return 0;
                  }

                  how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?

                  SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."


                  My Blog | Understanding State Pattern

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

                  The following will do what you want.

                  class MyClass
                  {
                  public:
                          MyClass(int a) : m_MyInt(a)     { }
                          MyClass() : m_MyInt(0)          { }
                  
                          void SetInt(int a)              { m_MyInt = a; }
                  private:
                          int m_MyInt;
                  };
                  
                  int main()
                  {
                          MyClass* pClass1 = new MyClass(6);
                          MyClass* pClass2 = new MyClass[5];
                  
                          for (int i = 0; i < 5; ++i)
                          {
                                  pClass2[i].SetInt(i);
                          }
                  
                          delete pClass1;
                          delete [] pClass2;
                  }
                  

                  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
                  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