logic question...

This is a discussion on logic question... within the C++ Programming forums, part of the General Programming Boards category; i got a class bank, with one data member of type account (other class)... i need to create a function ...

  1. #1
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    logic question...

    i got a class bank, with one data member of type account (other class)...
    i need to create a function grow that increases the number of acounts by a certain number...
    in order to dynamically allocate memory, i need to create a temp Bank object, copy all in it, delete the original object, recreate it with a larger number of accounts, and copy the data back in it and delete the temp object... right? sounds easy but i'm getting all confused because account is a class too... so... how do i go about this?
    i'm not interested in code, i just need to understant how it works.

    thanks.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you are using an array that has just enough memory for the number of accounts in the bank at any given time, thereby not "wasting" memory you aren't using, then yes, that's the protocol to add space for more accounts. Alternatively, you could allocated space for more accounts than you have, say space for 100 accounts, eventhough you only have 1 at the moment. Then you have to go through this exercise less often, and if the capacity of the array is large enough, then you don't have to do the expansion at all. Another alternative is to use an object of the STL vector class, which is essentially a self enlarging array. It will do all the expansion work for you, behind the scenes. Yet another option is to use a list. Lists are by nature of undetermined size, and are frequently used when you want to hold a group of something but you don't know how big the group is going to be when you write the program. You can write code for your own list, or use the STL list class to create a list object out of the box, which will work for most cases.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform game logic, problem
    By Akkernight in forum Game Programming
    Replies: 7
    Last Post: 02-23-2009, 10:49 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21