initialization list

Greetings,

in the example below Histomanager is a class that contains a variable fManager.

HistoManager *HistoManager::fManager = 0;

I don't undestand why for initiliazing fManager a pointer has been used.

Could the above code be written like taht:

HistoManager::fManager = 0;

Many thanks

closed account (yUq2Nwbp)
i can answer to your second question........if your fManager is static and public then it's all right but if fManager is non-static member then you can't access to it in that way from main...
Hi bbcc,

it looks as though HistoManager is part of a linked list where each HistoManager object contains a pointer, calledfManager to another HistoManager object. Is this what you want to do?

If you are within the scope of the class, and fManager is part of the class, then you shouldn't need to use scope operator ::
since it is implicit within the class.

You cant just declare fManager like


HistoManager::fManager = 0;


within HistoManager since its type is not declared, nor does it appear to be constant.

If you are within the class scope you can just initilise fManager in the initilaisation list:

1
2
3
4
5
6
7
8
9
10
11
class HistoManager
{
   HistoManager(...)
        : fManager(0), ...
      {
         ...
      }


...
};


If you are not within HistoManager then you need to specify the HistoManager object that you instantiated and that you wish to modify, e.g.,


int main()
{
HistoManager Example;
Example.fManager = 0; //if HistoManager::fManager is public then this is ok

...

}


Hope that helps.

Last edited on
fManager appears to be a static member. Since static members to not require an object of their containing class, they must be defined outside of the class. This is not an initialization list.
Hello everybody,

HistoManger is a class and inside it I have :

1
2
private:
 static HistoManager *fManager;

and the out of the scope of the class I have:

HistoManager *HistoManager::fManager = 0;

I sort of undesrtand it more now, Since it is a static variable a pointer has been used for it. But why moorecm says that
This is not an initialization list.
@moorecm
fManager appears to be a static member. Since static members to not require an object of their containing class, they must be defined outside of the class. This is not an initialization list.


This looks correct.

@bbcc
Since it is a static variable a pointer has been used for it


Just to be clear, just because the data member is static, it doesn't mean it must be a pointer, it could have any return type, e.g., void. It just means that this is a member that is associated with the HistoManager class, but not associated with any particular object, i.e., the this keyword is not applicable.

But why moorecm says that This is not an initialization list.

Because the initialisation list is used in class constructors to initialise its (non-static) data members, like I showed above.
So you mean that static memebers can't be initialized?
As I say above, static members are not associated with any particular object of the class, only the class itself. This allows you to have multiple functions/members with the same signature that in no way hide one another since you use the scope operator to call them with their full name (i.e., class_name::function_name()).

Constant static members of integral type can be assigned a value in the class, but it is still only a declaration not a definition, e.g.:

1
2
3
4
5
6
7
8
class Example
{
...

private:
   static const int example_member = 5;
   int array[example_member]; //example of use
};


which is fine to be used without providing a definition so long as you don't take its address.

If you take the address of the const static member then you need to provide a separate definition outside of the class (usually in the accompanying implementation file)

 
const int Example::example_member; //since example_member is const you can't declare its value again 


If you have a compiler that complains about the initialisation of constant static data members of integral type then
you can use the ``enum hack''.

Hope this helps.
Topic archived. No new replies allowed.