Solved

static member data

Posted on 2001-07-12
7
390 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Seeya
Comment Utility
ok now i understand thanks,Kotan
0
 
LVL 6

Expert Comment

by:kotan
Comment Utility
Don't you give me some point?
0
 

Author Comment

by:Seeya
Comment Utility
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
Comment Utility
>> 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

771 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

15 Experts available now in Live!

Get 1:1 Help Now