Why does this work has to do with const

Discussion in 'C++' started by Tony Johansson, Aug 15, 2005.

  1. 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
     
    Tony Johansson, Aug 15, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Earl Purple Guest

    Re: Why does this work has to do with const

    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).
     
    Earl Purple, Aug 15, 2005
    #2
    1. Advertising

  3. Tony Johansson

    Rolf Magnus Guest

    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
     
    Rolf Magnus, Aug 15, 2005
    #3
  4. > 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
     
    Tony Johansson, Aug 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,264
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    11
    Views:
    1,152
  3. Javier
    Replies:
    2
    Views:
    627
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,174
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    952
    fungus
    Oct 31, 2008
Loading...

Share This Page