3

So I have the following code

class UserDB
{
private:
AccountInfo* _accounts[200] ; // store up to 200 accounts
public:
UserDB();
virtual ~UserDB();
}
UserDB::UserDB(){
//code for initializing it to null
}
UserDB::~UserDB(){
    delete [] _accounts;
}

So basically I am trying to find this code to initialize _accounts to null but I cannot find a real answer, all the guides in the internet either say how to initialize an array, an object, or a pointer, but not something that is all three altogether, and even less how to initialize this kind of pointer to null, even whatever they are initializing [in the guides] looks very confusing, so I come once again to ask for help here. Also, AccountInfo is just any random class.

Ren
  • 4,594
  • 9
  • 33
  • 61
  • 1
    I've seen this array of accounts three or four times already this week. I'm not sure if anyone already mentioned this to you, but you won't have an easy time trying to learn C++ without [a good book](http://stackoverflow.com/q/388242/46642). You can definitely learn by posing questions here on SO, but it certainly shouldn't be your sole source. – R. Martinho Fernandes Feb 02 '12 at 07:05
  • What is a good book anyway? All the ones I have ever put eyes on are most of the time incomplete and are not detailed enough. – Ren Feb 02 '12 at 07:29
  • Yokhen, @R.MartinhoFernandes provided a link to [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) in his comment. – Blastfurnace Feb 02 '12 at 07:41

3 Answers3

4

use std::array or std::vector.

you don't delete[] _accounts because the array is a value -- it is an array of pointers. IOW, its size is not equal to a pointer.

Here's a std::vector approach:

class UserDB {
private:
    std::vector<AccountInfo*> _accounts;
public:
    UserDB() : _accounts(200, 0) {}
    virtual ~UserDB() {}
};

However, you may prefer to use the vector's default initializer so you can use it to determine the number of accounts it holds.

Update in response to comments below:

Although there are reasons to hold an array of AccountInfo* pointers, you may also consider std::vector to hold an array of AccountInfos values:

class UserDB {
private:
    std::vector<AccountInfo> _accounts;
public:
    UserDB() : _accounts() {}
    virtual ~UserDB() {}
    void appendAccountInfo(const AccountInfo& info) {
       this->_accounts.push_back(info);
    }
};

std::vector will handle all your allocation and reallocation needs for you. It's also nice because it's dynamically resizable, and you won't be constrained to a fixed number of AccountInfos.

justin
  • 104,054
  • 14
  • 179
  • 226
  • I have no clue what vectors are whatsoever. – Ren Feb 02 '12 at 06:48
  • Also, if I shouldn't use 'delete' could you provide me an example of how would I dispose of the dynamic array in the destructor? – Ren Feb 02 '12 at 06:57
  • @Yokhen if you want to write (and be able to read) conventional c++, you will need to know what `std::vector` is. it is a replacement for a resizable c array and very very very very common in c++ programs. for array with a fixed size, you can use `std::array` instead. `std::vector` provides the `delete[]` and resizing implementations for you. since you are using pointers: it will *not* automatically `delete` each `AccountInfo` in the array (vector). You can use `std::vector` to hold an array of `AccountInfo`s by *value*. – justin Feb 02 '12 at 06:58
  • 1
    @Yokhen: what dynamic array? There are no dynamic arrays in your code. – R. Martinho Fernandes Feb 02 '12 at 06:59
  • Well, the professor claimed that _accounts was a dynamic array. And yeah, sorry, I am researching as much as I can, but unfortunately I just started on C++ since 2 days ago. – Ren Feb 02 '12 at 07:08
  • @Yokhen no worries - there was a time I also thought "I have no clue what vectors are" =) – justin Feb 02 '12 at 07:11
2

create a constant instead of the magic number 200 (not really necessary but it makes the code more readable and safer when later changing)

const int numberOfAccounts = 200;

AccountInfo* _accounts[numberOfAccounts] 

UserDB::UserDB()
{
   for (int i = 0; i < numberOfAccounts; ++i)
   {
     _accounts[i] = 0;
   }
}

now you have you 200 zeroed pointers.

also have a habit of putting private members at the end of the class and public at the start, especially by bigger classes you want to see the public stuff first, the private stuff you normally don't want somebody to mess with.

class C
{
public:
protected:
private:
};
AndersK
  • 35,813
  • 6
  • 60
  • 86
0

I do remember having read that this would work:

UserDB():_accounts(){}

This should initialize the contents to NULL

mukeshkumar
  • 2,698
  • 3
  • 19
  • 20