By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Why does this work has to do with const

P: n/a
Hello Experts!

Why is it possible to modify Student when I have defined it as const
Student* s = new Student(1);
How should I do instad if I don't want to allow to change s so this
expression s->setNumber(0); should not be allowed.
If I compare with primitive data types instead.
If I instead have:
int var = 1;
const int k_var = 100;
const int* pointer_const;
int* int_pointer
then
pointer_const = &k_var; //this is correct
pointer_const = var; // this is also correct
int_pointer = &k_var; //this is error
*pointer_const = 0; /this is error

What I understand does this expression mean const Student* s = new
Student(1);
that what s points to can't be changed but it can according to
s->setNumber(0); strange????

I must have missed something here.
#include "student.h"
#include <iostream>
using namespace std;
int main()
{
const Student* s = new Student(1);
s->setNumber(0);
return 0;
}

class Student
{
public:
Student(long i) : number(i)
{}
void setNumber(long i) const
{
number = i;
}

private:
long number;
};

Many thanks

//Tony
Aug 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tony Johansson wrote:
Hello Experts!

Why is it possible to modify Student when I have defined it as const
Student* s = new Student(1);
How should I do instad if I don't want to allow to change s so this
expression s->setNumber(0); should not be allowed.
If I compare with primitive data types instead.
If I instead have:
int var = 1;
const int k_var = 100;
const int* pointer_const;
int* int_pointer
then
pointer_const = &k_var; //this is correct
pointer_const = var; // this is also correct
int_pointer = &k_var; //this is error
*pointer_const = 0; /this is error

What I understand does this expression mean const Student* s = new
Student(1);
that what s points to can't be changed but it can according to
s->setNumber(0); strange????
You made setNumber() const, which gives the promise that this function does
not modify the object. Therefore, the compiler lets you call it through the
pointer to const.
I must have missed something here.
#include "student.h"
#include <iostream>
using namespace std;
int main()
{
const Student* s = new Student(1);
s->setNumber(0);
return 0;
}

class Student
{
public:
Student(long i) : number(i)
{}
void setNumber(long i) const
{
number = i;
This should actually produce an error, because you're not allowed to modify
member variables from within a const member function. Which compiler are
you using?
}

private:
long number;
};

Many thanks

//Tony


Aug 15 '05 #2

P: n/a

Tony Johansson wrote:
Hello Experts!

If I compare with primitive data types instead.
If I instead have: int var = 1;
const int k_var = 100;
const int* pointer_const;
int* int_pointer then
pointer_const = &k_var; //this is correct
yes that is ok.
pointer_const = var; // this is also correct
No it isn't. var is type int, while pointer_cast is type const int *.
int_pointer = &k_var; //this is error
*pointer_const = 0; /this is error
Correct, both of those are errors.
What I understand does this expression mean const Student* s = new
Student(1);
that what s points to can't be changed but it can according to
s->setNumber(0); strange????
You can call that if setNumber is a const method. But it's unlikely
that such a method should be const.
I must have missed something here.
#include "student.h"
#include <iostream>
using namespace std;
int main()
{
const Student* s = new Student(1);
s->setNumber(0);
return 0;
}

class Student
{
public:
Student(long i) : number(i)
{}
void setNumber(long i) const
{
number = i; // ERROR IS IN THIS LINE
}

private:
long number;
};

Many thanks

//Tony


Your compiler should give an error but not in the line that calls
setNumber but in the line that I have flagged. Your const function
cannot modify "number" (unless number has been declared mutable, and on
this occasion it's almost certain you don't want it to be).

Aug 15 '05 #3

P: n/a
> This should actually produce an error, because you're not allowed to
modify
member variables from within a const member function. Which compiler are
you using?


In this setNumber I have forgotten to insert number = i;
Now I get en error which is correct as you pointed out to me.

//Tony
Aug 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.