Re: Initialize a const pointer to a constant object
Hank stalica wrote:
> So I have this class with a private data member:
>
> const Floor* const destFloor;
>
>
> I need to initialize f, which I'm trying to do in the constructor
> initializer list:
>
> Rider::Rider(const Floor& f)
> :destFloor(f)
> {
> }
>
> Here's my error message:
>
> Rider.cpp(17) : error C2440: 'initializing' : cannot convert from 'const
> Floor' to 'const Floor *const '
Well, that's right. You can't initialize a pointer with a reference to the
object.
> How do I initialize destFloor?
Take the address:
Rider::Rider(const Floor& f)
:destFloor(&f)
{
}
Or just make your member a reference, too. Since you chose to make your
pointer const, you can't let it point to anything else later anyway.
|