constructor invocation---other than the defaults?

This is a discussion on constructor invocation---other than the defaults? within the C++ Programming forums, part of the General Programming Boards category; Code: class myclass { int i,j; public: myclass():i(0){} myclass(int a):i(a),j(a){} myclass(int a,int b):i(a),j(b){} void put() { cout<<i<<" "<<j<<endl; } }; ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    12

    constructor invocation---other than the defaults?

    Code:
    class myclass
    {
    int i,j;
    public:
    myclass():i(0){}
    myclass(int a):i(a),j(a){}
    myclass(int a,int b):i(a),j(b){}
    void put()
    {
    cout<<i<<" "<<j<<endl;
    }
    };
    main()
    {
    myclass *obj=new myclass[5];
    }
    This works out well for def constructors. if i need the the other two constructors to be invoked what must be done here? Please help.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    myclass *obj=new myclass[5];

    when dynamicaly allocating memory for an array of objects,
    default ctor will be called for each object.

    there is no way to call another ctors.
    because other ctors will be requiring args which u can not pass to an array of objects.
    C/C++ IDE: Microsoft visual studio .Net 2003

    Wisdom is the reward for a lifetime of listening... when you'd have preferred to talk.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    12

    for an array object any ctor could be invoked?Right!!

    if it is an arrayObject then the constructor could be invoked like
    Code:
    myclass m[5]={1,2,3,4,5}
    for the second ctor, right? Can't it happen for an pointer then!!Help me.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    lydiapeter says
    if it is an arrayObject then the constructor could be invoked like

    Code:
    Code:
    myclass m[5]={1,2,3,4,5}
    for the second ctor, right? Can't it happen for an pointer then!!Help me.
    No, there is no way to do that....
    when dynamically allocating memory, default ctor will be called for each object.

    Yes, there is a way to do what u require..
    that is overload new[] and delete [].
    but that may be very cumbersome....


    enjoy.........
    C/C++ IDE: Microsoft visual studio .Net 2003

    Wisdom is the reward for a lifetime of listening... when you'd have preferred to talk.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by howhy
    Yes, there is a way to do what u require..
    that is overload new[] and delete [].
    This is not a solution to the problem. operator new [] is invoked to allocate raw memory for the array. The constructors for elements of the array are invoked (one by one) after operator new [] returns.

    The only general way to do what is wanted is to create the array, and then explicitly initialise the elements.

    For example, assuming myclass has an assignment operator that works appropriately;
    Code:
    int main()
    {
          myclass *obj = new myclass[5];
         for (int i = 0; i < 5; ++i)
         {
             myclass temp(i, i);
             obj[i] = temp;
         }
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You should use vector for dynamic arrays.

    In this case, with vector you can use any of the constructors, although all objects will be initialized with the same value.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by Daved
    You should use vector for dynamic arrays.
    I agree that it's often a good idea, but there's no "should" about it. It is up to the programmer to decide how to do things, and there are trade-offs with std::vectors (eg the fact that it automatically resizes itself) that can be help or hindrance in different situations.

    In this case, the examples using operator new[] are relevant to the discussion, not a claim that one should always use it

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    12
    thanks a lot

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> but there's no "should" about it

    I was referring to the OP, not to the discussion of operator new[]. By default, you should choose vectors to handle your dynamic array needs. There are always trade-offs and many factors that are important to these decisions, but a beginner is rarely equipped to weigh those factors accurately. In the absence of the ability to do that, you should simply use vectors for dynamic arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi-core affinity defaults
    By CodeMonkey in forum Tech Board
    Replies: 4
    Last Post: 12-08-2008, 02:47 PM
  2. Template Defaults
    By Cactus_Hugger in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2008, 01:25 AM
  3. Replies: 3
    Last Post: 10-15-2007, 12:50 PM
  4. Defaults for Constructor
    By vb.bajpai in forum C# Programming
    Replies: 1
    Last Post: 07-07-2007, 01:06 PM
  5. Unix - Checking defaults
    By john513 in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-31-2005, 07:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21