home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2745
Discussions: 188,385, Posts: 2,243,517, Members: 232,615
Old April 24th, 2003, 03:04 AM   Digg it!   #1 (permalink)
Senior Member
 
squeech's Avatar
 
Join Date: May 2002
Location: Rocky Mountain High
Posts: 613
Exclamation
Arrays and classes fight to the death

Hello again,

I've run into a brick wall while coding my new program. For some background info, this program is designed to be a simple simulator for overclocking CPUs (test what your chip can do before destroying it).

Anyways, I created a class so that each CPU is a declared object of the class. Inside the main program, there is an array of these objects strung together so that the user can test multiple CPUs if they so choose.

Here's some pseudocode to show you sort of how the program starts to flow.

//Start main()
Declare array of 5 objects of type CPU; //CPU is the class name
for(this loop lets each CPU be tested separately in squence)
{
User follows prompts to enter information about each CPU;
Program constructs the CPU object with data members according to given parameters from user;
}
... //The rest is not coded yet
//End main()

--------------------------------------------------------------------------------
And now for the problem at hand:
The problem comes in that I need to declare the array of CPUs before the user data is entered, and thus they objects are created using the default constructor in the class (in this case all members are set to zero). Then, after the data is entered, I need to reinitialize the CPU with the data...but I can't RECONSTRUCT the object using the paramaterized constructor! That's a compiler's worst nightmare and I get many errors. ARGGHH!

I'd like to find some way to do this without having to individually use modifier functions to enter in the data, but it is a last resort.

Any insight is GREATLY appreciated! TIA

P.S. I realize this is not the most efficient way to build this program, and if I did it again some things would be different. However, I have requirements to meet within this so things like the array can't be removed!

squeech is offline   Reply With Quote
Old April 24th, 2003, 06:44 PM     #2 (permalink)
Senior Member
 
squeech's Avatar
 
Join Date: May 2002
Location: Rocky Mountain High
Posts: 613
^bump^ Pleeeeeease
__________________
Talking in numbers doesn't make you smarter.

squeech is offline   Reply With Quote
Old April 24th, 2003, 07:10 PM     #3 (permalink)
Member
 
Join Date: Feb 2002
Posts: 161
Why not just use some sort of init() method that sets all of the required properties?

for each cpu
{
  get cpu info
  cpuArray[i].init(speed, vcore, will_start_on_fire, ...);
}

Martee is offline   Reply With Quote
Old April 24th, 2003, 07:23 PM     #4 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
Declaring an array doesn't mean you have to actually create the members.

CPU cpuArray[5];
...
cpuArray[i] = new CPU(thermal_mass, combustion_temperature, ...);
strangerstill is offline   Reply With Quote
Old April 24th, 2003, 07:35 PM     #5 (permalink)
Member
 
Join Date: Feb 2002
Posts: 161
That would only work if the array was an array of pointers (e.g. CPU* cpuArray[5]).
Martee is offline   Reply With Quote
Old April 24th, 2003, 09:08 PM     #6 (permalink)
Senior Member
 
squeech's Avatar
 
Join Date: May 2002
Location: Rocky Mountain High
Posts: 613
stranger, That was one thought I had was creating a dynamic array out of it (an array of pointers) instead of static, but wouldn't that cause the same problem? Doesn't declaring the array of pointers (dynamic array) stil construct each object just like a static array does? Or would it work since you're only declaring a pointer to that location?

I was under the impression that Dynamic and Static arrays would both cause the same issue, but I may be wrong!

Also, an init() function is definitely a way to go about it. That's a better resort than doing multiple member functions for each data member...I'll have to keep that in mind.

I am curious abou the pointers, though, if anyone knows??
squeech is offline   Reply With Quote
Old April 24th, 2003, 09:34 PM     #7 (permalink)
Member
 
Join Date: Feb 2002
Posts: 161
Quote:
Originally posted by squeech
Doesn't declaring the array of pointers (dynamic array) stil construct each object just like a static array does?
Yes, but the objects being constructed are now only pointers to CPU objects, not the CPU objects themselves. You can then create the CPU object that each pointer points to using the new operator and your parametrized constructor.
Martee is offline   Reply With Quote
Old April 24th, 2003, 11:26 PM     #8 (permalink)
Senior Member
 
squeech's Avatar
 
Join Date: May 2002
Location: Rocky Mountain High
Posts: 613
Wahoo! Setting up the dynamic array indeed worked beautifully! Thank you all again for the wonderful suggestions! What a relief!
squeech is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may 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
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2899)
CPU wont boot (7)
3-days in and no threads about Gaza (160)
I think I just killed my computer w.. (24)
hp compaq nc6000 problems (139)
Upgrading RAM (5)
Folderchat Weekday thread (442)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (15)
Recent Discussions
New Build ( Finally ) (3)
CPU wont boot (7)
Problem With LightScribe DVD Dr.. (0)
GLaDOS is up. (3)
HP notebook reinstall Vista NO .. (5)
Building a gaming computer advi.. (4)
hp compaq nc6000 problems (139)
Folderchat Weekday thread (442)
Creative T-3000 Subwoofer (3)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 11:12 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28