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. C++ related question

C++ related question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 Posts 3 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.
  • J Offline
    J Offline
    John theKing
    wrote on last edited by
    #1

    Hi friends ! Consider scenario

    class A
    {
    public:
    A()
    {
    cout << "In zero argument constructor";
    }

     A(int val)
      {
           cout << "In one argument constructor";
      }
    

    };

    void main()
    {

    A *obj;
    obj= new A[20];

    // Blah
    }

    As you can see above, in main i used statement obj= new A[20];. As such the zero argument constructor gets called. What i want is that this statement cause one argument constructor to get called. How is it possible ?? ThankX. Bye Bye

    T A 2 Replies Last reply
    0
    • J John theKing

      Hi friends ! Consider scenario

      class A
      {
      public:
      A()
      {
      cout << "In zero argument constructor";
      }

       A(int val)
        {
             cout << "In one argument constructor";
        }
      

      };

      void main()
      {

      A *obj;
      obj= new A[20];

      // Blah
      }

      As you can see above, in main i used statement obj= new A[20];. As such the zero argument constructor gets called. What i want is that this statement cause one argument constructor to get called. How is it possible ?? ThankX. Bye Bye

      T Offline
      T Offline
      Taka Muraoka
      wrote on last edited by
      #2

      You can't. What would it call each one with?


      Software is everything. It also sucks. Charles Fishman [^] Awasu 1.0.2 (beta)[^]: A free RSS reader with support for Code Project.

      1 Reply Last reply
      0
      • J John theKing

        Hi friends ! Consider scenario

        class A
        {
        public:
        A()
        {
        cout << "In zero argument constructor";
        }

         A(int val)
          {
               cout << "In one argument constructor";
          }
        

        };

        void main()
        {

        A *obj;
        obj= new A[20];

        // Blah
        }

        As you can see above, in main i used statement obj= new A[20];. As such the zero argument constructor gets called. What i want is that this statement cause one argument constructor to get called. How is it possible ?? ThankX. Bye Bye

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        Technically it's not possible, but you can certainly work around it. The easiest way is to get rid of your default constructor and give your other constructor a default value for its parameter: A(int val = 17) { ... } You can also go with more complex solutions, such as global or static member variables which you initialize before creating the array, and then perhaps increment inside the constructor as each element is created. No matter what you do, all elements of the array will call the same constructor. There's no way out of it. Regards, Alvaro


        When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

        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