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.
-
1I'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. You can definitely learn by posing questions here on SO, but it certainly shouldn't be your sole source.R. Martinho Fernandes– R. Martinho Fernandes2012年02月02日 07:05:22 +00:00Commented Feb 2, 2012 at 7: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– Ren2012年02月02日 07:29:23 +00:00Commented Feb 2, 2012 at 7:29
-
Yokhen, @R.MartinhoFernandes provided a link to The Definitive C++ Book Guide and List in his comment.Blastfurnace– Blastfurnace2012年02月02日 07:41:32 +00:00Commented Feb 2, 2012 at 7:41
3 Answers 3
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.
6 Comments
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<AccountInfo> to hold an array of AccountInfos by value.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:
};
2 Comments
UserDB::UserDB() : _accounts().I do remember having read that this would work:
UserDB():_accounts(){}
This should initialize the contents to NULL