LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2004, 12:59 PM   #1
lazyboy0001
Member
 
Registered: Mar 2004
Posts: 31

Rep: Reputation: 15
const in C++


Anybody can give me a detail desctiption about const in C++? Or you can
just tell me where I can find a document about it. I always get confused
about where I should use const or not, especially for the member functions
in a class. And sometime, a missing const may cause compilation error
later for those that use the class.
 
Old 04-08-2004, 01:05 PM   #2
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
http://cpptips.hyperformix.com/Const.html
 
Old 04-08-2004, 01:13 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Basically, the C++ "const" qualifier turns a "variable" into a "constant". It specifies the variable to be read-only; it declares "you cannot modify this!".

All well and good. It's a "better way" than using an old C-style "#define"; it's similar to a Java "static final" qualifier. And, as you noted, you cannot initialize a "const" variable with anything other than a "constant expression" (otherwise you get those compiler errors you mentioned).

The tricky part is if you use "const" in pointer declarations.

Here are a couple of examples that might clarify things a bit:

// Here, the *pointer* is a const:
char *const ptr = mybuf; // const pointer
*ptr = 'a'; // This is OK
ptr = yourbuf; // ERROR: pointer is const

// Here, the string *pointed to* is a const:
const char *ptr = mybuf; // Pointer to const
ptr = yourbuf; // This is OK
*ptr = 'a'; // ERROR: *ptr is const

Hope that helps .. PSM
 
Old 04-08-2004, 02:16 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
You can also have const member functions for classes by adding the const keyword to the end. const member functions are guaranteed not to change the state of the object. That is, they cannot change member variables for that class.
 
Old 04-08-2004, 03:42 PM   #5
lazyboy0001
Member
 
Registered: Mar 2004
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks for all your helps. Based on your suggestion, I performed the test to variable definations
and here is my test result.

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 2;

const int *ptr0 = &b; // (*ptr0) is const
ptr0 = &a;
// *ptr0 = b; // invalid

int * const ptr1 = &b; // ptr1 is const
*ptr1 = a;
// ptr1 = &a; // invalid

const int * const ptr2 = &b; // both ptr2 and (*ptr2) are const
// ptr2 = &a; // invalid
// *ptr2 = a; // invalid

int const * ptr3 = &b; // (*ptr3) is const, same to ptr0
ptr3 = &a;
// *ptr3 = a; // invalid

int const * const ptr4 = &b; // both ptr4 and (*ptr4) are const
// ptr4 = &a; // invalid
// *ptr4 = a; // invalid

int const *ptr5; // (*ptr5) is const but ptr5 not
ptr5 = &a;
// *ptr5 = b; // invalid

// int * const ptr6; // ptr6 is const, it need to be initialized
// ptr6 = &a; // invalid
// *ptr6 = b; // nonsense since ptr6 is not initialized

return 0;
}

If there are still more const variable defination method, please let me know.
And I am working on the const member function now.
 
Old 04-08-2004, 04:43 PM   #6
lazyboy0001
Member
 
Registered: Mar 2004
Posts: 31

Original Poster
Rep: Reputation: 15
Here is the test of const member functions

#include <iostream>
using namespace std;

template <typename T>
class Data
{
public:
Data();
Data(const Data<T> &d);

void setData(T const &d);
const T getData() const;
const T getData0();
const T getData1() const;

private:
T myData;
};

template <typename T>
Data<T>:ata() {}

template <typename T>
Data<T>:ata(const Data<T> &d) { myData = d.getData(); }

template <typename T>
void Data<T>::setData(T const &d) { myData = d; }

template <typename T>
const T Data<T>::getData() const
{
return myData;
}

template <typename T>
const T Data<T>::getData0()
{
myData = 3;
return myData;
}

template <typename T>
const T Data<T>::getData1() const
{
myData = 3;
return myData;
}

int main()
{
Data<double> theData;
theData.setData(5);

cout<<theData.getData()<<endl; // output: 5
cout<<theData.getData0()<<endl; // output: 3
// cout<<theData.getData1()<<endl; // can't get compiled.

Data<double> newData(theData);
cout<<theData.getData()<<endl; // output: 3
cout<<newData.getData0()<<endl; // output: 3
// cout<<newData.getData1()<<endl; // can't get compiled.

return 0;
}

The const after the mem function do restricted the access to mem variable.
My new question is, it seems that I can just ignore the const before the mem functions.
like, I can change 'const T getData() const; ' to 'T getData() const; '.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: #define OR const int ? Hady Programming 5 10-07-2005 11:43 AM
Non-Const reference? ChemicalBurn Programming 4 04-05-2005 08:32 AM
c++ ERROR: int to const char ashirazi Programming 6 08-06-2004 10:01 PM
Saving an Ip to a Const? Half_Elf Linux - Networking 3 07-12-2002 08:20 PM
conversion to non-const reference type max2878 Linux - General 2 05-23-2002 01:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration