member variable constant after construction

B

bvcheung

Hi,

I have a member variable of a class that should not be changed after
construction. How can I ensure this?

-Billy
 
J

Jonathan Mcdougall

I have a member variable of a class that should not be changed after
construction. How can I ensure this?

Make it const.

class c
{
private:
const int ci;

public:
c(int v)
: ci(v)
{
ci = 10; // error
}
};


Jonathan
 

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,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top