decay bug

F

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.
 
J

John Carson

Fraser Ross said:
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 *.
 
F

Fraser Ross

"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 *.


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.
 
V

Victor Bazarov

Fraser said:
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
 
V

Victor Bazarov

Fraser said:
"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 *.


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
 
J

John Carson

Fraser Ross said:
"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 *.


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top