image
Physics Forums Logo
image
image
* Register * Upgrade Blogs Library Staff Rules Mark Forums Read
image
image   image
image

Go Back   Physics Forums > Science Education > Homework & Coursework Questions > Engineering, Comp Sci, & Technology


Reply

image Urgent! C++ How to declare/initialize a constant in a class? Share It Thread Tools Search this Thread image
Old Jul16-07, 07:46 PM                  #1
Mr Virtual

Mr Virtual is Offline:
Posts: 218
Question Urgent! C++ How to declare/initialize a constant in a class?

1. The problem statement, all variables and given/known data
I have my computer science exam today (in 2 hours). I want to know how to declare and initialize a const member in a class.

3. The attempt at a solution

I tried this:

__________________________________________________ _______________
class stu
{
const int a=10; // Error cannot initialize a data member here
};
--------------------------------------------------------------------------

Then I tried this:
__________________________________________________ _______________
class stu
{
const int a;
};

int stu::a=10; //Error : Multiple declaration of stu::a
--------------------------------------------------------------------------

Then this:
__________________________________________________ _______________
class stu
{
const int a;

stu() //constructor
{
a=10; //Error: Cannot modify a const member
}

};

--------------------------------------------------------------------------

Please help!!!!

Thanks
Mr V
  Reply With Quote
Old Jul16-07, 07:53 PM                  #2
Mr Virtual

Mr Virtual is Offline:
Posts: 218
I am using Turbo C++ 3.01

Mr V
  Reply With Quote
Old Jul16-07, 08:16 PM                  #3
out of whack

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.
  Reply With Quote
Old Jul16-07, 08:40 PM                  #4
chroot

PF Admin
 
chroot's Avatar

chroot is Offline:
Posts: 10,248
You're just missing the 'static' keyword.

- Warren
  Reply With Quote
Old Jul16-07, 08:42 PM                  #5
Mr Virtual

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
  Reply With Quote
Old Jul16-07, 08:47 PM                  #6
Mr Virtual

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
  Reply With Quote
Old Jul16-07, 08:48 PM                  #7
chroot

PF Admin
 
chroot's Avatar

chroot is Offline:
Posts: 10,248
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
  Reply With Quote
Old Jul16-07, 08:52 PM                  #8
Mr Virtual

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
  Reply With Quote
Old Jul16-07, 08:56 PM                  #9
chroot

PF Admin
 
chroot's Avatar

chroot is Offline:
Posts: 10,248
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
  Reply With Quote
Old Jul16-07, 08:58 PM                  #10
Mr Virtual

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
  Reply With Quote
Old Jul16-07, 09:00 PM                  #11
chroot

PF Admin
 
chroot's Avatar

chroot is Offline:
Posts: 10,248
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
  Reply With Quote
Old Jul16-07, 09:07 PM                  #12
Mr Virtual

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
  Reply With Quote
Old Jul16-07, 09:10 PM                  #13
chroot

PF Admin
 
chroot's Avatar

chroot is Offline:
Posts: 10,248
Good luck!

- Warren
  Reply With Quote
Old Jul17-07, 07:28 PM                  #14
Mr Virtual

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.
  Reply With Quote
Old Jul17-07, 07:31 PM                  #15
Mr Virtual

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
  Reply With Quote
Old Jul17-07, 07:31 PM                  #16
chroot

PF Admin
 
chroot's Avatar

chroot is Offline:
Posts: 10,248
Mr Virtual,

Those things don't matter anyway. No one uses puts(). I'm almost amazed teachers test on such useless things!

- Warren
  Reply With Quote
image image
Reply
Thread Tools


Similar Threads for: Urgent! C++ How to declare/initialize a constant in a class?
Thread Thread Starter Forum Replies Last Post
IV diode charateristic, Find constant A , Urgent mack90mg Engineering, Comp Sci, & Technology 0 Mar24-08 01:02 AM
IV diode charateristic, Find constant A , Urgent mack90mg Engineering, Comp Sci, & Technology 0 Mar24-08 12:57 AM
Why not declare war to corrupted politicians? lockecole Politics & World Affairs 4 Dec3-05 02:58 PM
Initialize private members Jim Roberts Computing & Technology 3 Oct12-04 02:40 AM
Will Hawking declare BH info puzzle solved? marcus Beyond the Standard Model 16 Jul17-04 08:48 PM

Powered by vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd. © 2009 Physics Forums
Sciam | physorgPhysorg.com Science News Partner
image
image   image