The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Dynamic Size for a private member
Discuss Dynamic Size for a private member in the C Programming forum on Dev Shed. Dynamic Size for a private member C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 18th, 2002, 04:36 PM
|
|
Member
|
|
Join Date: Oct 2000
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Dynamic Size for a private member
Hi,
Let's say I have a class called "CDlibrary", and inside this class, I have a class called "CD" as a private member. An instance of CDlibrary can contain multiple CDs. I don't know how many CDs I have before I initialize the CDlibrary instance. How do I design the CDlibrary class so that different instances can contain different # of CDs?
eg. If every library has 1 CD then:
class CDlibrary {
private:
CD disc1;
};
Thanks for your help in advance.
|

March 18th, 2002, 06:10 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
You could try declaring a pointer to an array of class CD. In the class constructor for CDLibrary, you can initialize the pointer to null. Then, when you know how many CDs you have, you can allocate the memory as needed. Also, in your destructor, you can check if you allocated memory to hold the CDs or not and destroy the array appropriately E.g.
Code:
class CDlibrary {
private:
CD *discs;
int nDiscs;
public:
CDLibrary();
~CDLibrary();
void CreateCDs(int num);
};
CDLibrary::CDLibrary() {
discs = NULL;
}
CDLibrary::~CDLibrary() {
if (discs) {
delete discs;
discs = NULL;
}
}
void CDLibrary::CreateCDs(int num) {
nDiscs = discs;
discs = new CD[num];
}
|

March 18th, 2002, 06:18 PM
|
|
Member
|
|
Join Date: Oct 2000
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Ah stupid me. Somehow I thought the CD instances in the array would be deleted after the CreateCDs() function exits...
Thanks for your help =)
|

March 18th, 2002, 11:19 PM
|
|
Contributing User
|
|
Join Date: Mar 2002
Location: Oxford, UK
Posts: 28
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Um....
When you delete an array, you MUST use
delete [] discs;
The reason is that delete discs will only delete the first element, so you will leave most of your allocated memory hanging.
|

March 19th, 2002, 12:28 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Oops, my bad... Nice catch!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|