![]() |
|
#1
|
|||
|
|||
simple class variables questionHi there.
If I had a class with the following sorts of methods involving passing around other classes, is it better to keep the variables as pointers or just regular variables? I mean, should I do something like this: CPP / C++ / C Code:
or like this? CPP / C++ / C Code:
Thanks. Last edited by LuciWiz : 01-Jun-2005 at 23:40.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
|||
|
|||
|
I'm no expert, but I'll say this.
One of the reasons you want dynamic allocation is when you don't know how many objects you will need at runtime. So if your class a could have 7 times and 3 dates, that might be a good reason to choose it. If your class a is always going to have exactly 1 time and 1 date, then why bother? Perhaps if the times or dates are really large and/or contain stuff that may vary in size. Generally, I'd go with the first. If you don't know how many class a's you will need, then dynamically allocate each as you need it. And when you do so, they will have exactly what you need inside them. If it's extra work for the sake of a performance hit, I say sh!tcan it. Or something like that. |
|
#3
|
|||
|
|||
|
I would say use regular variables unless, as Dave said, you don't know how many you will need until runtime. Using dynamic allocation creates so many headaches such as pointer dereferencing, deallocation problems, access violations, scope problems, sharing problems, etc, so why bother if it's unnecessary? Also, using a pointer won't save any space because the data still has to exist somewhere, so you are actually creating overhead by requiring 32 (or however many) bits for a pointer.
|
|
#4
|
||||
|
||||
|
Hi edpoole.
I usually use dynamic allocation, but only if it makes sense. In your class, you have date and time variables. There should only be one of these for each instance of A. Therefore, there is no need to make these pointers, just use normal variables. However, you may have several instances of your class A. This is where I would use pointers and dynamic memory, not in the class itself. Cheers, d __________________
The best damn Sports Blog period. |
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Opinion on my code and a c++ class question | FlipNode | C++ Forum | 7 | 07-Feb-2006 08:15 |
| non-member function question | crq | C++ Forum | 1 | 03-Feb-2005 21:59 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | C++ Forum | 2 | 19-Dec-2004 06:06 |
| Function and Array (w/ reference variables) question | brookeville | C++ Forum | 15 | 07-Dec-2004 01:11 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The