 |
Jul16-07, 07:53 PM
|
#2
|
Mr Virtual is
Offline:
Posts: 218
|
I am using Turbo C++ 3.01
Mr V
|
|
|
|
Jul16-07, 08:16 PM
|
#3
|
out of whack is
Offline:
Posts: 463
|
IIRC, when you don't need external linkage you can do this:
class stu { static const int a=10; };
If you need external linkage (eg you're building a library that will be linked to other modules) you're safer with this form:
class stu { static const int a; };
const int stu::a=10;
...I think.
|
|
|
|
Jul16-07, 08:40 PM
|
#4
|
chroot is
Offline:
Posts: 10,260
|
You're just missing the 'static' keyword.
- Warren
|
|
|
|
Jul16-07, 08:42 PM
|
#5
|
Mr Virtual is
Offline:
Posts: 218
|
Thanks a lot! It works perfectly.
But I am a little confused as to why the keyword 'const' is preceded by 'static'. Can't I just create a const without making it a static? I am asking this because value of a const cannot be changed, then why do we need to make it a static?
Mr V
|
|
|
|
Jul16-07, 08:47 PM
|
#6
|
Mr Virtual is
Offline:
Posts: 218
|
You're just missing the 'static' keyword.
|
Yeah, I realised that. But can you explain why we need to make it a static here?
Is this to make the objects able to share the const.
Mr V
|
|
|
|
Jul16-07, 08:48 PM
|
#7
|
chroot is
Offline:
Posts: 10,260
|
A static member belongs to the class; it is shared among all instances of the class. A non-static member belongs to a specific instance.
- Warren
|
|
|
|
Jul16-07, 08:52 PM
|
#8
|
Mr Virtual is
Offline:
Posts: 218
|
One more question: if I declare a member function as 'static const' what does it mean? I know that since it is static, it is restricted to accept/access static data members only. But what effect does maknig it a 'const' have on it? Will it accept/access only const values?
Mr V
|
|
|
|
Jul16-07, 08:56 PM
|
#9
|
chroot is
Offline:
Posts: 10,260
|
Static and const mean two different things. Static members belong to the class, rather than specific instances of the class. Static data isn't necessarily constant, though. A constant member is one that is created with a specific value, and whose value cannot ever be changed.
- Warren
|
|
|
|
Jul16-07, 08:58 PM
|
#10
|
Mr Virtual is
Offline:
Posts: 218
|
Sure you are right. But does making a function constant restrict it to access only const members?
Mr V
|
|
|
|
Jul16-07, 09:00 PM
|
#11
|
chroot is
Offline:
Posts: 10,260
|
No. If a function is constant, then it returns something (perhaps an object) which cannot be changed by the caller. It doesn't matter what data the function uses to create its return value.
- Warren
|
|
|
|
Jul16-07, 09:07 PM
|
#12
|
Mr Virtual is
Offline:
Posts: 218
|
Oh!Oh! How did I forget that! I forgot that the datatype preceding function name specifies its return type. So, if I declare a function const, it will return a const value.
Thanks a lot to you, and 'out of whack' for all your help. I hope I would fare well in my exam. Bye then!
Mr V
|
|
|
|
Jul16-07, 09:10 PM
|
#13
|
chroot is
Offline:
Posts: 10,260
|
Good luck!
- Warren
|
|
|
|
Jul17-07, 07:28 PM
|
#14
|
Mr Virtual is
Offline:
Posts: 218
|
Thanks!
I performed well. Only forgot that 'setw' is in 'iomanip.h' and that puts() automatically includes a newline character at the end of a string.
|
|
|
|
Jul17-07, 07:31 PM
|
#15
|
Mr Virtual is
Offline:
Posts: 218
|
I have my chemistry paper next, and I have a terrible confusion in Henry's law of solubility. I am creating the thread in the homework section (chemistry). Please help me there...
Mr V
|
|
|
|
Jul17-07, 07:31 PM
|
#16
|
chroot is
Offline:
Posts: 10,260
|
Mr Virtual,
Those things don't matter anyway. No one uses puts(). I'm almost amazed teachers test on such useless things!
- Warren
|
|
|
|
|
 |
 |
|
 |
|