need an explanation on a function decoration

O

Onix

Hi guys,

I'm reading a code and I saw this function decoration for a test class
(in the test.h file)

const int *getitem() const;

The associated one in test.cpp file

const int *test::getitem() const {return &blah; }

What I want to know is the role of each "const" here. I'm confused. If
anyone can help, I really appreciate
 
L

lbonafide

const int *test::getitem() const {return &blah; }

The first const means getitem() returns a const int *, the second
means getitem() can't modify any of test's member variables.
 
A

Abhishek Padmanabh

Hi guys,

I'm reading a code and I saw this function decoration for a test class
(in the test.h file)

const int *getitem() const;

The associated one in test.cpp file

const int *test::getitem() const {return &blah; }

What I want to know is the role of each "const" here. I'm confused. If
anyone can help, I really appreciate

The first const (going from left to right) together with int* means,
it is a pointer to const int (or a const array of int - pointer to its
first item). The second const says that the member function getitem()
does not change the non-mutable state of the object. That is, getitem
will not modify anything in the object and hence, the state of the
objects remains the same before and after the call to getitem(). See
the FAQ as well : http://www.parashift.com/c++-faq-lite/const-correctness.html
 
O

Onix

The first const (going from left to right) together with int* means,
it is a pointer to const int (or a const array of int - pointer to its
first item). The second const says that the member function getitem()
does not change the non-mutable state of the object. That is, getitem
will not modify anything in the object and hence, the state of the
objects remains the same before and after the call to getitem(). See
the FAQ as well :http://www.parashift.com/c++-faq-lite/const-correctness.html

Thanks, that really clears things up
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top