Solved

static member data

Posted on 2001-07-12
7
409 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

  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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

839 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