Class instance declaration in C++
Let开发者_C百科's say I have ClientList class and I have declared like this.
class ChatMgr
{
private:
ClientList _userlist;
ClientList *_userlist;
}
Then what is the difference? I know that the second one is the address of the instance and I need to initialize it using new to use it. Then for the first one, can I just access to the all the data members inside of the class without initializing it?
Thanks in advance....
You are correct, _userList
is an actual instance of the ClientList
class, so is initialised when ChatMgr
is (its constructor is called), but *_userlist
is a pointer, which is left uninitialised.
What is the difference?
ClientList _userlist;
Adds an object of class ClientList
as an member of the class ChatMgr
_userlist
is an object in itself so, the compiler will call it's constructor when object of ChatMgr
is created. All initializations of _userlist
should occur in it's own constructor
or Initializer list
of the constructor.
ClientList *_userlist;
Adds an pointer to object of class ClientList
as an member of the class ChatMgr
*_userlist
should be point to something valid before using it because it is just a pointer and you need to point it to something meaninful to be able to use it.
Preferably, You should use initialize in Initializer List
of the Class ChatMgr
.
For the first one, can I just access to the all the data members inside of the class without initializing it?
As explained in Q1, Yes you can access the contents of _userlist
without initializing it because the initialization happens implicitly when compiler calls ClientList
constructor.
If i remember correctly, the first one goes through the constructor and creates the ClientList, and the second only saves an address for an potential ClientList.
Since you haven't assigned something to point at, the latter will be unusable until you created the object and assigned its new address to the pointer.
ClientList _userlist;
- _userlist
is an object.
ClientList *_userlist;
- _userlist
is a pointer to an object of type ClientList
.
In the first case, you can access the data members as long as it sees the definition of the ClientList
before the instantiation of the current ChatMgr
object. So,
class ClientList
{
// ...
};
class ChatMgr
{
ClientList _userList ;
// ...
};
I'm concerned you might mixed up the concepts. Keep in mind the difference between:
House H;
House *L;
H stores a house in itself. L stores addresses of Houses.
You need to initialise the pointer before using it, but the instance will probably get initialised in your constructor.
Your class has 2 members:
ClientList _userlist;
ClientList *_userlist;
ClientList _userlist
is an instance of ClientList
. Its constructor will be invoked by the ChatMgr
constructor, and assuming that constructor initialises everything properly, you can start using it.
ClientList *_userlist
is a pointer to an instance of ClientList
. It mist get initialised before you can use it, either by using new
, or by assigning it the address of some other instance. Once you do this, the pointer will be initialised, and the instance to which it points will probably be initialised too (new
will call the ClientList
constructor).
Changing the names of the member variables, so that the code would actually compile:
class ChatMgr
{
private:
ClientList _userlist;
ClientList *_pUserlist;
}
You can never access any object unless it's initialized. The difference is that _userList
will be initialized whenever you create a ChatMgr
object, whether you do it explicitly or not, so there is no (legal) way to get hold of such a member without it being initialized.
_pUserlist
, OTOH, is a pointer, a POD, and won't be initialized at all (not even to 0
/NULL
) unless you do it explicitly.
精彩评论