decay bug

Discussion in 'C++' started by Fraser Ross, Jul 17, 2005.

  1. Fraser Ross

    Fraser Ross Guest

    class DataBuffer {
    enum { bufferSize=0x20000 };
    unsigned char buffer_[bufferSize];
    public:
    unsigned char * const Buffer() const {
    return buffer_;
    };
    };

    I get the error "cannot convert unsigned char const * to unsigned char *"
    with the Buffer function. Is this a compiler bug?

    Fraser.
    Fraser Ross, Jul 17, 2005
    #1
    1. Advertising

  2. Fraser Ross

    John Carson Guest

    "Fraser Ross" <> wrote in message
    news:1121625327.27ee15d1534a9e3f9cf69d058cd52891@teranews
    > class DataBuffer {
    > enum { bufferSize=0x20000 };
    > unsigned char buffer_[bufferSize];
    > public:
    > unsigned char * const Buffer() const {
    > return buffer_;
    > };
    > };
    >
    > I get the error "cannot convert unsigned char const * to unsigned
    > char *" with the Buffer function. Is this a compiler bug?
    >
    > Fraser.


    Move the const to before the * rather than after the *.


    --
    John Carson
    John Carson, Jul 17, 2005
    #2
    1. Advertising

  3. Fraser Ross

    Fraser Ross Guest

    "John Carson"
    > "Fraser Ross"> > class DataBuffer {
    > > enum { bufferSize=0x20000 };
    > > unsigned char buffer_[bufferSize];
    > > public:
    > > unsigned char * const Buffer() const {
    > > return buffer_;
    > > };
    > > };
    > >
    > > I get the error "cannot convert unsigned char const * to unsigned
    > > char *" with the Buffer function. Is this a compiler bug?
    > >
    > > Fraser.

    >
    > Move the const to before the * rather than after the *.
    >
    >
    > --
    > John Carson



    That can't be the return type because data should be modifyable. The
    functions constness affects the error. It compiles when I remove it. I
    have const and non-const Buffer functions now.

    Fraser.
    Fraser Ross, Jul 17, 2005
    #3
  4. Fraser Ross wrote:
    > class DataBuffer {
    > enum { bufferSize=0x20000 };
    > unsigned char buffer_[bufferSize];
    > public:
    > unsigned char * const Buffer() const {
    > return buffer_;
    > };
    > };
    >
    > I get the error "cannot convert unsigned char const * to unsigned
    > char *" with the Buffer function. Is this a compiler bug?


    No. Your 'buffer_' is an array of char. In a constant object
    (*this in a member declared 'const') that's an array of const char.
    You cannot return a pointer to a non-const char from a member that
    is declared const.

    V
    Victor Bazarov, Jul 17, 2005
    #4
  5. Fraser Ross wrote:
    > "John Carson"
    >> "Fraser Ross"> > class DataBuffer {
    >>> enum { bufferSize=0x20000 };
    >>> unsigned char buffer_[bufferSize];
    >>> public:
    >>> unsigned char * const Buffer() const {
    >>> return buffer_;
    >>> };
    >>> };
    >>>
    >>> I get the error "cannot convert unsigned char const * to unsigned
    >>> char *" with the Buffer function. Is this a compiler bug?
    >>>
    >>> Fraser.

    >>
    >> Move the const to before the * rather than after the *.
    >>
    >>
    >> --
    >> John Carson

    >
    >
    > That can't be the return type because data should be modifyable.


    You can't expect to declare your 'DataBuffer' const and at the same
    time allow modifications to it.

    > The
    > functions constness affects the error. It compiles when I remove it.


    Of course. You're only allowed to modify the 'buffer_' (through the
    pointer which you return) if the object itself is non-const.

    > I have const and non-const Buffer functions now.


    Good.

    V
    Victor Bazarov, Jul 17, 2005
    #5
  6. Fraser Ross

    John Carson Guest

    "Fraser Ross" <> wrote in message
    news:1121626339.b8eed5c518467f0ef98217690fec72b6@teranews
    > "John Carson"
    >> "Fraser Ross"> > class DataBuffer {
    >>> enum { bufferSize=0x20000 };
    >>> unsigned char buffer_[bufferSize];
    >>> public:
    >>> unsigned char * const Buffer() const {
    >>> return buffer_;
    >>> };
    >>> };
    >>>
    >>> I get the error "cannot convert unsigned char const * to unsigned
    >>> char *" with the Buffer function. Is this a compiler bug?
    >>>
    >>> Fraser.

    >>
    >> Move the const to before the * rather than after the *.
    >>
    >>
    >> --
    >> John Carson

    >
    >
    > That can't be the return type because data should be modifyable. The
    > functions constness affects the error. It compiles when I remove it.
    > I have const and non-const Buffer functions now.


    I don't follow your reasoning. When the function is const, the data is not
    modifiable (that is what a const function means) and so the return type must
    be a pointer to const char.

    If the function is not const, then you can drop const from the return type
    entirely.


    --
    John Carson
    John Carson, Jul 17, 2005
    #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. Mantorok Redgormor

    array decay

    Mantorok Redgormor, Sep 23, 2003, in forum: C Programming
    Replies:
    1
    Views:
    384
    Kevin Bracey
    Sep 23, 2003
  2. Mantorok Redgormor

    array decay

    Mantorok Redgormor, Oct 8, 2003, in forum: C Programming
    Replies:
    2
    Views:
    427
    Barry Schwarz
    Oct 9, 2003
  3. pandapower

    array to pointer decay question !!

    pandapower, Feb 2, 2004, in forum: C Programming
    Replies:
    5
    Views:
    562
    Horst Kraemer
    Feb 2, 2004
  4. frederik aa de jonge

    module for calculations involving radioactive decay?

    frederik aa de jonge, Nov 2, 2006, in forum: Python
    Replies:
    0
    Views:
    393
    frederik aa de jonge
    Nov 2, 2006
  5. m sergei

    array to pointer decay question !!

    m sergei, Jun 30, 2004, in forum: C Programming
    Replies:
    6
    Views:
    422
    Dan Pop
    Jul 1, 2004
Loading...

Share This Page