Solved

static member data

Posted on 2001-07-12
7
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

751 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