User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,509 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,105 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 326 | Replies: 3 | Solved
Reply
Join Date: Jul 2008
Posts: 11
Reputation: RandV80 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
RandV80 RandV80 is offline Offline
Newbie Poster

Using constructor within a class array

  #1  
Jul 22nd, 2008
I've built a class, one parent and multiple children, that I need to group together to eventually be stored in a single file. For the moment I've gone with something like this:

  1. CObject *group //CObject is the class in question
  2.  
  3. group = (CObject*)calloc(1, sizeof(CObject)); //1 element just to start off

This compiles, and using "group[0]." calls the variables and methods from the class, which also compiles. But what I can't seem to access is the constructor. My class has both a blank default constructor and the standard initialized all variable constructor, the latter which I want to use. However, "group[0]." does not bring up the constructor option, and "group[0]()" does not compile. So how do I use my constructor?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Using constructor within a class array

  #2  
Jul 22nd, 2008
>But what I can't seem to access is the constructor.
This is where the C memory functions fall flat, they don't work well with constructors and destructors. I recommend ditching calloc and using new[]:
  1. CObject *group = new CObject[N];
Or better yet, use the vector container class if you plan on resizing the collection:
  1. #include <vector>
  2.  
  3. ...
  4.  
  5. std::vector<CObject> group ( 1 ); // Start off with 1 element
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jul 2008
Posts: 620
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: Using constructor within a class array

  #3  
Jul 22nd, 2008
The calloc (and malloc) function never calls constructors; free() never calls destructor(s).
So using new and delete operators for class objects (or array of class objects) is not only recommendation or a good style symptom - it's one of the very strict C++ language specifications.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: RandV80 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
RandV80 RandV80 is offline Offline
Newbie Poster

Re: Using constructor within a class array

  #4  
Jul 22nd, 2008
Thanks, I'll give those a try. My OO coding is rusty and I forgot about these.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC