GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jun-2005, 19:34
edpoole edpoole is offline
Invalid Email Address
 
Join Date: Feb 2005
Posts: 3
edpoole is on a distinguished road

simple class variables question


Hi 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:

class a {
public:
 Date getDate() { return date; }
 void setDate(Date d) { date = d; }
 Time getTime() { return time; }
 void setTime(Time t) { time = t; }
private:
 Time time;
 Date date;
};

or like this?

CPP / C++ / C Code:
class a {
public:
 a() {
   time = NULL;
   date = NULL;
  }
 ~a() {
   if(time != NULL) delete time;
   if(date != NULL) delete date;
  }
 Date* getDate() { return date; }
 void setDate(Date* d) {
   if(date != null) delete date;
   date = new Date();
   date.setDate(&d);
  }
 Time* getTime() { return time; }
 void setTime(Time* t) {
   if(time != null) delete time;
   time = new Time();
   time.setTime(&t);
  }
private:
 Time* time;
 Date* date;
};

Thanks.
Last edited by LuciWiz : 01-Jun-2005 at 23:40. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 01-Jun-2005, 20:41
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
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  
Old 04-Jun-2005, 16:19
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
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  
Old 05-Jun-2005, 07:32
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.