const data member in the class

A

Atharvan!!!

first of all accept my apologies for bad english.
(I am not a native english speaker)

I am a 1st year B.Sc CS student.

What I understood is follows

1) A const data member must be initialized at the time of declaration.
2) Datamembers of a class cannot be initialized in the class
declaration itself.
(ie., we need either the help of constructer or use a
memberfunction.)

Am I correct?

Then my doubt is that, how can we declare a const data member in a
class.
(suerly we cannot it initialize it that time.It is against the 1st
statement
if we can initialize it there it is against the 2nd statement.)

Please help me.


Arun
[
Atharvan!!!
{333}
<a href:"http://chintaadaara.blogspot.com">Chintaadaara</a>
]
 
R

red floyd

Atharvan!!! said:
first of all accept my apologies for bad english.
(I am not a native english speaker)

I am a 1st year B.Sc CS student.

What I understood is follows

1) A const data member must be initialized at the time of declaration.
2) Datamembers of a class cannot be initialized in the class
declaration itself.
(ie., we need either the help of constructer or use a
memberfunction.)

Am I correct?

Then my doubt is that, how can we declare a const data member in a
class.
(suerly we cannot it initialize it that time.It is against the 1st
statement
if we can initialize it there it is against the 2nd statement.)

Please help me.
What book are you using that doesn't discuss initialization lists?
Google for it, or see the FAQ section 10
http://www.parashift.com/c++-faq-lite/ctors.html
 
A

Andrey Tarasevich

Atharvan!!! said:
...
What I understood is follows

1) A const data member must be initialized at the time of declaration.

Wrong. This doesn't make any sense.
2) Datamembers of a class cannot be initialized in the class
declaration itself.
(ie., we need either the help of constructer or use a
memberfunction.)

Constant data member of the class can only be initialized through the
constructor initializer lists.
Then my doubt is that, how can we declare a const data member in a
class.

Just declare it.
(suerly we cannot it initialize it that time.

Of course we can't. _Declarations_ (the non-defining ones) can't have
initializers and not even supposed to have any.
It is against the 1st statement

Your 1st statement doesn't make sense.
 
J

Jerry Coffin

[ ... ]
1) A const data member must be initialized at the time of declaration.
2) Datamembers of a class cannot be initialized in the class
declaration itself.
(ie., we need either the help of constructer or use a
memberfunction.)

Am I correct?

A data member must be initialized, but not necessarily directly in its
declaration. It can (and usually must) be initialized by the
constructor. For example:

class X {
const int y;
public:
X() : y(1) {}
};

Note that this must be done with the member initialization syntax like
above, NOT an assignment in the constructor body.

There is one exception: you're allowed to initialize a member as part of
its definition when/if it's a static const member:

class X {
static const int y = 10;
};

This feature was added rather late in the original C++ standardization
process, so many old compilers don't support it. The classic workaround
is known as the enum hack. It looks like:

class X {
enum some_tag { y = 10 };
};
 
T

tommy.hinks

first of all accept my apologies for bad english.
(I am not a native english speaker)

I am a 1st year B.Sc CS student.

What I understood is follows

1) A const data member must be initialized at the time of declaration.
2) Datamembers of a class cannot be initialized in the class
declaration itself.
(ie., we need either the help of constructer or use a
memberfunction.)

Am I correct?

Then my doubt is that, how can we declare a const data member in a
class.
(suerly we cannot it initialize it that time.It is against the 1st
statement
if we can initialize it there it is against the 2nd statement.)

Please help me.

Arun
[
Atharvan!!!
{333}
<a href:"http://chintaadaara.blogspot.com">Chintaadaara</a>
]

You can do it like this:

// a.hpp
class A
{
public:
A(const int arg_m);
private:
const int m;
};

// a.cpp
A::A(const int arg_m) : m(arg_m)
{}

So, m is initialized in the so-called member initialization list.
There really is nothing more to it.
 
A

Atharvan!!!

first of all accept my apologies for bad english.
(I am not a native english speaker)
I am a 1st year B.Sc CS student.
What I understood is follows
1) A const data member must be initialized at the time of declaration.
2) Datamembers of a class cannot be initialized in the class
declaration itself.
(ie., we need either the help of constructer or use a
memberfunction.)
Am I correct?
Then my doubt is that, how can we declare a const data member in a
class.
(suerly we cannot it initialize it that time.It is against the 1st
statement
if we can initialize it there it is against the 2nd statement.)
Please help me.
Arun
[
Atharvan!!!
{333}
<a href:"http://chintaadaara.blogspot.com">Chintaadaara</a>
]

You can do it like this:

// a.hpp
class A
{
public:
A(const int arg_m);
private:
const int m;

};

// a.cpp
A::A(const int arg_m) : m(arg_m)
{}

So, m is initialized in the so-called member initialization list.
There really is nothing more to it.

Thank you for find sometime for me.
Can you suggest any method to connect to databases.(I want it a one
time study,because I want to use it in both windows and linux,)
I am using Dev C++ in windows and Anjuta IDE in linux.
Hope you will help me:)
with love
Arun
[
Atharvan!!!
{333}
httP://chintaadara.blogspot.com
]
 
Joined
Mar 9, 2008
Messages
4
Reaction score
0
man thats easy i will show u how
first of all
usually const data members is used when u have an object as a private member not integer or double....etc

and yes constructors do initialize everything now i will post to u a simple program to demonstrate it using 1 class and 1 main(main means driver)
for any questions pleas dont hesitate to email me (e-mail address removed)

#include<iostream.h>
class person{

private:

const int age;
const double weight;

public:

person(const int a=0,const double b=0):age(a),weight(b)
{

}

void print()
{
cout<<"The age is: "<<age<<endl;
cout<<"The weight is: "<<weight<<endl;
}

};

int main ()
{

person p(28,75.5);
p.print();
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top