2

Let'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....

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167

7 Answers7

2

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.

Node
  • 3,443
  • 16
  • 18
1

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

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.

Allman
  • 41
  • 4
0

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 ;
     // ...
 };
Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

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).

Dominic Gurto
  • 4,025
  • 2
  • 18
  • 16
  • 1
    `*_userlist` is *not* a pointer. The pointer in the second declaration is `_userlist` (without the star). `*_userlist` is the *pointee* (that is, the thing being pointed to). – Konrad Rudolph Jun 14 '11 at 06:50
0

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.

Guido Tarsia
  • 1,962
  • 4
  • 27
  • 45
  • “pointer variables will all have the same size” – not true. For instance, member function pointers don’t have the same size as non-member function pointers. Furthermore, pointer sizes may actually vary depending on the architecture and as far as I know there’s no guarantee that arbitrary pointer types have the same size on a given architecture (also, FAR and NEAR pointers come to mind). Furthermore, this is just detracting from the real issue here, which is automatic storage vs. pointers. – Konrad Rudolph Jun 14 '11 at 06:52
0

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.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445