Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

static member data

Posted on 2001-07-12
7
Medium Priority
?
436 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 150 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

927 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