Problem returning const pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kk_oop@yahoo.com

    Problem returning const pointer

    Hi. I just wrote a function that returns a const pointer:

    MyClass
    {
    ....
    public:
    virtual ReturnClass * const getReturnClass( );
    ....
    }

    I then wrote some code that calls the function to make sure the
    pointer was really const:

    MyClass myClass1;
    ReturnClass * myReturnClass = myClass1.getRet urnClass( );

    MyClass myClass2;
    myReturnClass = myClass2.getRet urnClass( );


    I anticipated getting a compiler error when I attempted to reset
    myReturnClass, but this did not happen. The compiler let me change
    the value. What did I do wrong?

    Thanks!

    Ken


  • Pete Becker

    #2
    Re: Problem returning const pointer

    On 2008-10-22 18:05:27 -0400, kk_oop@yahoo.co m said:
    Hi. I just wrote a function that returns a const pointer:
    >
    MyClass
    {
    ...
    public:
    virtual ReturnClass * const getReturnClass( );
    ...
    }
    >
    I then wrote some code that calls the function to make sure the
    pointer was really const:
    >
    MyClass myClass1;
    ReturnClass * myReturnClass = myClass1.getRet urnClass( );
    myReturnClass is not a const pointer. Without seeing the definition of
    myClass1 it's not possible to say what's going on. Which is why the FAQ
    recommends including a minimal code sample that shows the problem,
    rather than a summary that omits the key parts.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)

    Comment

    • Thomas J. Gritzan

      #3
      Re: Problem returning const pointer

      kk_oop@yahoo.co m schrieb:
      Hi. I just wrote a function that returns a const pointer:
      >
      MyClass
      {
      ...
      public:
      virtual ReturnClass * const getReturnClass( );
      ...
      }
      Top-level const on return values is quite useless, since you make a copy
      of the returned value anyway.

      What do you want to accomplish by that?
      I then wrote some code that calls the function to make sure the
      pointer was really const:
      >
      MyClass myClass1;
      ReturnClass * myReturnClass = myClass1.getRet urnClass( );
      >
      MyClass myClass2;
      myReturnClass = myClass2.getRet urnClass( );
      myReturnClass is not const.

      It's like this:

      const int i_const = 5;
      int i2 = i_const;
      i2 = 7; // i2 is not const!

      i2 contains a copy of i_const, but is not const itself.
      I anticipated getting a compiler error when I attempted to reset
      myReturnClass, but this did not happen. The compiler let me change
      the value. What did I do wrong?
      Consider using references more and pointers less.

      --
      Thomas

      Comment

      • James Kanze

        #4
        Re: Problem returning const pointer

        On Oct 23, 12:05 am, kk_...@yahoo.co m wrote:
        Hi. I just wrote a function that returns a const pointer:
        MyClass
        {
        ...
        public:
        virtual ReturnClass * const getReturnClass( );
        ...
        }
        I then wrote some code that calls the function to make sure the
        pointer was really const:
        MyClass myClass1;
        ReturnClass * myReturnClass = myClass1.getRet urnClass( );
        MyClass myClass2;
        myReturnClass = myClass2.getRet urnClass( );
        I anticipated getting a compiler error when I attempted to
        reset myReturnClass, but this did not happen. The compiler
        let me change the value. What did I do wrong?
        Why would you anticipate a compiler error? myReturnClass isn't
        const, so there's no reason you shouldn't be able to modify it.
        How is this any different from:

        int const c = 43 ;
        int i = c ;

        For non-class types, the const keyword is ignored for rvalues;
        an rvalue of a non-class type is never "cv-qualified". (For
        class types, it is relevant, since you can call member functions
        on an rvalue; if the rvalue is const, you can only call const
        member functions on it.) The return value of a function is an
        rvalue, so there's really no point in declaring it const. (Note
        that except for calling a member function on it, there is no
        possible way to modify an rvalue.)

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...