Need help understanding a const function returning an object reference.

Discussion in 'C++' started by noobzillaking@gmail.com, Sep 21, 2007.

  1. Guest

    If I have the following code:

    struct Pencil
    {
    int Sharpness;
    };
    class random
    {
    public:
    Pencil& GetPencil() const {return p;}
    Pencil p;
    };

    Why doesn't this compile.... when.....

    struct Pencil
    {
    int Sharpness;
    };
    class random
    {
    public:
    Pencil& GetPencil() const {return *(Pencil*)&p;}
    Pencil p;
    };

    Does compile.

    Is there something weird about references that I don't know about?
     
    , Sep 21, 2007
    #1
    1. Advertising

  2. Barry Guest

    Re: Need help understanding a const function returning an objectreference.

    wrote:
    > If I have the following code:
    >
    > struct Pencil
    > {
    > int Sharpness;
    > };
    > class random
    > {
    > public:
    > Pencil& GetPencil() const {return p;}
    > Pencil p;
    > };
    >
    > Why doesn't this compile.... when.....


    As inside the const member function, /p/ is typeof "const Pencil",
    your return type of "GetPencil" is "Pencil&", "const Pencil" is not
    convertible to "Pencil&"

    in the latter program, while const member functions are not allowed to
    modify any none-mutable member nor to call non-const member functions,
    taking the address of a member is not in this scope, type-casting is not
    in this scope either.


    --
    Thanks
    Barry
     
    Barry, Sep 21, 2007
    #2
    1. Advertising

  3. Daniel T. Guest

    wrote:

    > If I have the following code:
    >
    > struct Pencil
    > {
    > int Sharpness;
    > };
    > class random
    > {
    > public:
    > Pencil& GetPencil() const {return p;}
    > Pencil p;
    > };
    >
    > Why doesn't this compile.... when.....


    The above doesn't compile because you are exposing a member through a
    const function. The member should be exposed as const. As in:

    const Pencil& GetPencile() const { return p; }

    > struct Pencil
    > {
    > int Sharpness;
    > };
    > class random
    > {
    > public:
    > Pencil& GetPencil() const {return *(Pencil*)&p;}
    > Pencil p;
    > };
    >
    > Does compile.


    The above does compile because you are using a c-style cast to remove
    the const from 'p'.
     
    Daniel T., Sep 21, 2007
    #3
  4. jonyman Guest

    On Sep 21, 2:14 pm, "Daniel T." <> wrote:
    > wrote:


    >
    > The above does compile because you are using a c-style cast to remove
    > the const from 'p'.


    I hadn't considered that C casts could remove const ness (I thought
    const casting (through const_cast) was a new addition to c++).
    Regardless: Thank you both for your help.
     
    jonyman, Sep 21, 2007
    #4
  5. Re: Need help understanding a const function returning an objectreference.

    jonyman wrote:
    > I hadn't considered that C casts could remove const ness (I thought
    > const casting (through const_cast) was a new addition to c++).


    C-style casting works as static_cast, reinterpret_cast and const_cast
    (hence the general guideline to avoid C-style casts, because they are
    confusing and can contain hidden surprises). The only new addition in
    C++ is dynamic_cast.
     
    Juha Nieminen, Sep 21, 2007
    #5
  6. Earl Purple Guest

    On 21 Sep, 10:21, Juha Nieminen <> wrote:
    > jonyman wrote:
    > > I hadn't considered that C casts could remove const ness (I thought
    > > const casting (through const_cast) was a new addition to c++).

    >
    > C-style casting works as static_cast, reinterpret_cast and const_cast
    > (hence the general guideline to avoid C-style casts, because they are
    > confusing and can contain hidden surprises). The only new addition in
    > C++ is dynamic_cast.


    Generally casts are evil. (See the FAQ).

    C++ casts are less evil than C casts because you are less likely to
    cast away something you didn't intend.

    The only time I normally use a C-style cast is if I'm passing a null
    pointer (0) to a function that has more than one overload and I want
    to tell the compiler what type of pointer I am passing, so

    overloaded_func( (const Foo *) 0 );

    which is harmless and really there is no need for

    overloaded_func( static_cast< const Foo * >( 0 ) );

    which is more confusing because it looks like you are converting
    something but in reality you are simply clarifying a literal.

    I'm not sure if the new standard is going to bring in a typed null
    pointer literal. I know that you'll be able to represent a null
    pointer unambiguous from integer 0 but not sure it will be totally
    typesafe.
     
    Earl Purple, Sep 21, 2007
    #6
    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. Jim Langston
    Replies:
    2
    Views:
    451
    Rolf Magnus
    May 11, 2006
  2. Javier
    Replies:
    2
    Views:
    571
    James Kanze
    Sep 4, 2007
  3. George2
    Replies:
    10
    Views:
    602
    Pete Becker
    Dec 17, 2007
  4. Jens Thoms Toerring

    Const/non-const pointer returning method

    Jens Thoms Toerring, May 25, 2010, in forum: C++
    Replies:
    11
    Views:
    724
    John H.
    May 26, 2010
  5. JustMe
    Replies:
    1
    Views:
    175
    Tassilo v. Parseval
    Aug 29, 2003
Loading...

Share This Page