Does a Function's Const-ness Influence the Return Type?
As the title says, does the const-ness of a member functions influence the type it returns? I have this simple class:
1 2 3 4 5
|
struct X
{
X(int *i_ = nullptr) noexcept : p_(i_) { }
int *p_;
};
|
In another class, I have a simple member function that returns a pointer to a non-constant type:
1 2 3 4 5 6
|
struct Base
{
/*...*/
int *base() const { return(this->b_); }
int b_[2];
};
|
This is all and good. However, when I attempt to call
Base::base(), I get a conversion error:
1 2 3 4 5
|
int main()
{
Base b_ = {1, 2};
b_.base(); // Error here.
}
|
The error I get is this:
Compiler wrote: |
---|
error: invalid conversion from 'const int*' to 'int*'" |
Is it the const-ness of
Base::base() that's causing this?
Thanks :)
Wazzak
It's not related to the return type, but it's not allowed to hand out such a pointer (or reference) to a member in a const member function.
I should've guessed that much. Thanks for clearing that up for me, Athar :)
Wazzak
Topic archived. No new replies allowed.