Results 1 to 6 of 6

Thread: Selecting a constructor to use with new in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    47

    Selecting a constructor to use with new in C++

    I am confused a little bit since i am not sure whether to use constructor or while allocating arrays of classes or structs with the new operator in c++ programming language. But i am unable to do so. Is it possible to select a constructor to use with new operator. Please help me pertaining to this matter. Need help.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Selecting a constructor to use with new in C++

    Seems you want to dynamically allocate arrays of some data types. It would have been better if you have posted the whole program or a part or segment of a program. Since unless and until i can see the program i cannot predict a proper solution. So in order to get a better solution i would recommend you to post your code.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Selecting a constructor to use with new in C++

    As far as i know , since you cannot dynamically allocate arrays with the new[] operator i think that it is not possible in the C++ programming language. You can achieve this by using the default constructor only. So try to use the default constructor in your programming code and i think that this will fix your problem.

  4. #4
    Join Date
    Dec 2009
    Posts
    47

    Re: Selecting a constructor to use with new in C++

    Firstly i am very much thankful to you all for the replies. Can you put some more light on this topic because if this is not possible then it clearly indicates that we cannot allocate arrays of a data type dynamically that does not contain a parameter less constructor. Thanks for clearing this but please explain in some more detail.

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: Selecting a constructor to use with new in C++

    According to my knowledge you should loop your new array and simply replace all the elements with an object which is initialized by the constructor which you actually want. It may lower your performance but nevertheless your problem will be solved. So i would recommend you to do it if you want to get rid of this problem.

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: Selecting a constructor to use with new in C++

    Most probably you will not able to achieve this without looping the array and after looping it simply replace it with the initialized object. According to me this will not affect your performance because unless and until your object is not using around 150 MB of memory and which is looped about 1500 times the performance will not be affected.

Similar Threads

  1. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  2. What is the use of private constructor in C++?
    By Juaquine in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 04:41 PM
  3. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  4. Constructor vs Method
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 01:22 PM
  5. use of methods and constructor
    By beelow in forum Software Development
    Replies: 4
    Last Post: 14-11-2009, 01:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,699,576.43605 seconds with 16 queries