Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > decay bug

Reply
Thread Tools

decay bug

 
 
Fraser Ross
Guest
Posts: n/a
 
      07-17-2005
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.


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      07-17-2005
"Fraser Ross" <(E-Mail Removed)> wrote in message
news:1121625327.27ee15d1534a9e3f9cf69d058cd52891@t eranews
> 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
 
Reply With Quote
 
 
 
 
Fraser Ross
Guest
Posts: n/a
 
      07-17-2005
"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.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-17-2005
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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-17-2005
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


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      07-17-2005
"Fraser Ross" <(E-Mail Removed)> wrote in message
news:1121626339.b8eed5c518467f0ef98217690fec72b6@t eranews
> "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

 
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
module for calculations involving radioactive decay? frederik aa de jonge Python 0 11-02-2006 04:07 AM
array to pointer decay question !! m sergei C Programming 6 07-01-2004 10:59 AM
array to pointer decay question !! pandapower C Programming 5 02-02-2004 01:38 PM
array decay Mantorok Redgormor C Programming 2 10-09-2003 03:32 AM
array decay Mantorok Redgormor C Programming 1 09-23-2003 12:45 PM



Advertisments