• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ multiple classes

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Zerix01

Member
Joined
Mar 12, 2007
I'm teaching my self c++ and have been doing pretty well but I'm stuck on something for a small project I'm doing. I have a class set up that I want to be able to create multiple instances of. I understand how to make how ever many number of a class I want by giving it a new name. The problem is that the total of instances of this class won't be determined until the programs runtime. So can I make a class name out of a variable? Any help would be great
 
Read up on the C++ new() operator. Dynamic memory allocation like malloc() in C.
 
class example
{
};

example *p;

p = new example;
// or
p = new example [howmany];

delete p;
 
Another way just to help while you are learning is instead of dynamically allocating the memory, just make a static array that is way bigger than you think you might need.

class example
{
};

example p[100]; //if you think 100 is the most you could possibly ever need


Note: this is not a recommended programming practice, but it can help you take one hurdle at a time while learning.
 
Thanks, hopefully I get a chance to work on this tomorrow. I did come across the new() operator while searching for this but nothing I found was terribly clear as to how to use it.

Another way just to help while you are learning is instead of dynamically allocating the memory, just make a static array that is way bigger than you think you might need.

Note: this is not a recommended programming practice, but it can help you take one hurdle at a time while learning.

Which is what I've been doing but as you said it is not recommended programming practice. It's bothering me in an OCD sort of way that I'm doing it like that, lol.
 
There are 2 ways to instantiate an object in C++, statically and dynamically. Static is determined at compile time, dynamic is allocated at run time on the heap as needed.

All variable names must be fixed at compile time. You cannot input a variable, for example, and create an object of that name. You can however create a pointer and make a new object pointed to by that pointer while the program is running. You can make as many as you need until you run out of memory on the heap.

Read up on pointers and the new/delete commands. Don't forget to clean up after yourself and return memory to the heap when you are done with it.
 
Thanks guys. I found a good example for what I'm trying to do, I also looked back and brushed up on pointers a bit (never saw a use for them until now).

http://cplusplus.com/doc/tutorial/dynamic/

class example
{
};

example *p;

p = new example;
// or
p = new example [howmany];

delete p;

This helped me out and the above link helps explain what is doing what. Looks like [howmany] can be a variable who's value can be set at runtime. Arrays were ****ing me off because they had to be set at compile time. I did however find an example of some code dealing with a pointer to the array then copying it to a new array of a larger size. But this is much cleaner.

Something I can't find any information on

Code:
delete[] p;

Will delete all objects created that p points to. What if I wanted to delete one and not all of them?
 
Last edited:
Yes, the number in brackets used with new can be a variable. It does not have to be set at compile time.

Actually, I think that p = new example[x] creates a new array on the heap of pointers to example objects, but not the objects themselves. You then would use a for loop to go through and run a new command on each one of those pointers to create an object for each one. You are using a pointer to an array of pointers... a p** basically. Like you can create 2 dimensional arrays, but it requires a for loop, even if one dimension is static, as new just creates an array of pointers to arrays (a char **). It doesn't create a new object off each one. To access them you must dereference twice.
 
Like you can create 2 dimensional arrays, but it requires a for loop, even if one dimension is static, as new just creates an array of pointers to arrays (a char **). It doesn't create a new object off each one. To access them you must dereference twice.

Sorry I'm still trying to pick up some of the terminology and like I said I only just gave myself a crash course in points yesterday. No it doesn't create a new object with

Code:
p = new example[x];

But I am able to access the data stored in them just as I would with any class array.

Code:
p = new example[x];

x = 2;  // creates three objects 0, 1, 2
y = 5;

example[1].setData(y);

cout << "Output is " << example[1].getData(); 




Output is 5

So if I understand you correctly, then no I don't have to dereference it twice. It lets me access the data as a class constructed with an array.
 
That is entirely correct... forgot about that, but now that you say it it is coming back... Creating an array using new calls the default constructor, and that is one of the main reasons you should have a default constructor.

Sorry about that, I guess my C++ is getting rusty. Thanks pharoer for the reminder/correction.
 
@Zerix01: read a book. Or several. E.g. http://www.ebooknetworking.com/-c-27.html

I picked up a C++ book called C++ in easy steps. It was a very good start for me and is a good quick reference since it is so small and direct to the examples. At least one of the examples was wrong or missing some chunk of code that it needed to run, but that was kind of fun since I was able to identify what was missing and add in the code.

Thanks for the link, I see they also have Java books too. That will be my next language I want to learn, mainly because I want to develop Android apps.
 
Back