Initialize a const pointer to a constant object

H

Hank stalica

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 '

How do I initialize destFloor?

Best Regards,
--Hank Stalica
 
J

Jim Langston

Hank stalica said:
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)

: destFloor(&f)
 
R

Rolf Magnus

Hank said:
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.
 
H

Hank Stalica

Thanks, Jim.
I actually figured that out shortly after posting.
Basically, it was one of those things I knew but it was under my nose.

Thanks.
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top