•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
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:
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?
C++ Syntax (Toggle Plain Text)
CObject *group //CObject is the class in question 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?
>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[]:
Or better yet, use the vector container class if you plan on resizing the collection:
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[]:
cplusplus Syntax (Toggle Plain Text)
CObject *group = new CObject[N];
cplusplus Syntax (Toggle Plain Text)
#include <vector> ... 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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Return Array from C++ (C++)
- How to use this typedef in nesting class. (C++)
- Constructor Question? (C#)
- bubble sort function w/ array (C++)
- Newbie Constructor / Array question (C++)
- constructor with an array as an argument (C++)
- Class with dynamic array how? (C++)
- sorting through an array of objects (Java)
- template array initialization (C++)
Other Threads in the C++ Forum
- Previous Thread: Installer
- Next Thread: Difference between foo() and foo(void)?



Linear Mode