Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why does this work has to do with const

Reply
Thread Tools

Why does this work has to do with const

 
 
Tony Johansson
Guest
Posts: n/a
 
      08-15-2005
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


 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      08-15-2005

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).

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-15-2005
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


 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      08-15-2005
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments