GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 125 of 203    Print  
Explain how static class member initialized?

  
Total Answers and Comments: 9 Last Update: July 31, 2009     Asked by: anoop4real 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: OSaienni
 
Everyone has forgotten, there is one type of class member that can be initialised within a class.

That's the constant integral data type.

eg.

class CInitialise
{
    static const int m_cInt = 10;

    static const char m_cChar = 's';

    static const long m_cLong = 1000000000;

     static const long long m_cLongLong = 2000000000000000;

};

Those all work.



Above answer was rated as good by the following members:
zeal_goswami, prapra, yzesong
March 26, 2007 19:47:52   #1  
sudha        

RE: Explain how static class member initialized?
using Scope resolution operator.

<class name>::<static var>

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 27, 2007 05:52:07   #2  
anoop4real Member Since: March 2007   Contribution: 8    

RE: Explain how static class member initialized?
Hi
Thanks for the info but can it be initialized with in the class?

 
Is this answer useful? Yes | No
April 09, 2007 20:37:05   #3  
Amit        

RE: Explain how static class member initialized?
Static variables can not be initialized in a class as class is building block of the object and you can not allocate memory for class but of class object. Static variable needs to be initialized outside the class.
 
Is this answer useful? Yes | No
June 07, 2007 01:38:45   #4  
divya shukla        

RE: Explain how static class member initialized?

Static class member initialized to zero when the first object of the class is created no further initialisation is required.
for example
class text
{
};
void main()
{
text t1.t2; // (comment)here the first object is created so static class member initialised to zero.
}


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
March 05, 2008 06:41:58   #5  
rabi.k.mishra Member Since: March 2008   Contribution: 1    

RE: Explain how static class member initialized?
Static members of a class are usually initialized outside the definition of class block.
Look at the following:

class static_member_class {
public:
static int i;
}

int static_member_class::i 0;

In the above example static_member_class has a static member data 'i' of integer type. However the static member data is initialized out of the class definition withe class scope.

Here many people argue here that while initializing the static member specifying datatype (here 'int') is optional. In the above example we could have initialized the variable like

static_member_class::i 0;

and this is perfectly fine for any standard c++ compiler.
But specidying the datatype is required when we are declaring a static member data of any user defined type. In such situations we have to explicitly specify the user defined data type before initializing the static data.

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
June 10, 2008 11:01:05   #6  
Prasad2008 Member Since: June 2008   Contribution: 8    

RE: Explain how static class member initialized?
static members can not be initialised within the class. Because what appears inside the class is just a declaration (note that declaring the static member within the class is mandatory along with it you also have to define it separately outside the class). Static member has to be defined and initialised outside the class(if you wont initialise its default value will be zero).

class sample{

static int a;

};
int sample::a 3;

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
June 18, 2008 09:02:44   #7  
chiyuwang Member Since: June 2008   Contribution: 4    

RE: Explain how static class member initialized?
static class member can be initialized by static constructor
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
September 12, 2008 06:41:23   #8  
OSaienni Member Since: September 2008   Contribution: 7    

RE: Explain how static class member initialized?
Everyone has forgotten there is one type of class member that can be initialised within a class.

That's the constant integral data type.

eg.

class CInitialise
{
static const int m_cInt 10;

static const char m_cChar 's';

static const long m_cLong 1000000000;

static const long long m_cLongLong 2000000000000000;

};

Those all work.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 30, 2009 22:43:23   #9  
yzesong Member Since: July 2009   Contribution: 20    

RE: Explain how static class member initialized?
Static variables of a class can only be initialized like this CLASS_NAME::STATIC_VAR_NAME VAR_VALUE;


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape