Solved

static member data

Posted on 2001-07-12
7
397 Views
Last Modified: 2008-03-04
Hi,
I have a class named Stack.I have an array of int data
type.I want to make the size of the array const and static and want to make a instance data of the class.
I have done this

class Stack
{
private:
static const int size = 20;
int st[size];
};

This works fine.But according to the rule in C++ i cannot assign a member data and especially static member data inside a  class so how is it that this particular piece of code works?Afetr intializing in the private part of the class.Also i would like to know with an eg as what is the difference between initializing a varilable and assigning a variable?

Regards,
Seeya

0
Comment
Question by:Seeya
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6279518
That can be done, because that variable is "const".
You can try to remove const keyword, and found it desn't work.

Initializing means you register a type to a variable,
     int variable;

Assigning means give a value to that variable
     variable = 10;

for this,
     int variable = 10;

means Initializing & Assigning are made together.
0
 

Author Comment

by:Seeya
ID: 6279543
Well does taht mean that if the data member of a class is const you can assign a  value to it in the private part of the class?
Secondly when you initialite the data members of  class in a constructor does it mean that you a assigning value to it in the constuctor.
suppose i have a constuctor

Class Omega
{
private:

int hours;
int minutes;
public:
 omega(int h, int m): hours(h), minutes(m)
{
}

};

void main()
{

  Omega om(5, 10);

}
In this constructor am initializing a value to the data members or assigning could you explain me in detail pls?

Thanks,
Seeya
0
 
LVL 6

Accepted Solution

by:
kotan earned 50 total points
ID: 6279569
For the keyword "const", it means the variable is a constant variable, it couldn't be changed later. So you must have a value on it on the beginning. It's no matter if it in private or public.


second,
For the line Omega om(5, 10),
it has initialized om to Omega. At the same time, it also initialized the member hours & minute. And than, the value 5 and 10 will be assigned to that member variable

Regards,
kotan
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Seeya
ID: 6279588
ok now i understand thanks,Kotan
0
 
LVL 6

Expert Comment

by:kotan
ID: 6279592
Don't you give me some point?
0
 

Author Comment

by:Seeya
ID: 6279602
I have accepted your comment as a answer & i am satisfied with it.I have already give the points!
If that was not the procedure do mail me back.
Regards,
Seeya
0
 
LVL 22

Expert Comment

by:nietod
ID: 6280339
>> Initializing means you register a type to a variable, int variable;
No.  "declaring" is when you specify an identifier's type.
"initialzaing" is when you provide it with its initial value.

>>     int variable = 10;
>> means Initializing & Assigning are made together.
No.  No assignment is used there.  that is a "definiton"  It "declares" an identifier, "defines" it and "iniitalize"s it.

>> Well does that mean that if the data member of a class
>> is const you can assign a  value to it in the
>> private part of the class?
If a non-static data member is constant you can initialize the data value inside the initialization list of the constructor.  This is the only place where you can set the member' value.  Like

class X
{
    const int i;

    X(jnt IniI) : i(IniI) {}
};

If a constant data member is static, it must be initialized 1 time, in the scope that encloses the class declaration.  Like

class X
{
    static const int i;
}

const int X::i = 4;

This appears to be the case you have.

>> n this constructor am initializing a value to the data
>> members or assigning
You are initlaizing them.  They are being creted ("constructed") with a specified value.  Thus they are being initialized.  If  you change their value later with operator = that is an assignment.   Note that code like

sometype somevariable = somevalue;

is not assignment and the operator = is not used.  That is construction.  somevariable will be constructed using the specified value.   (this assumes that somevalue is of type sometype.)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now