473,803 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialize a const pointer to a constant object

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(co nst 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
Oct 15 '07 #1
3 4791

"Hank stalica" <me@nospam.netw rote in message
news:69******** *************** *******@comcast .com...
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(co nst Floor& f)
:destFloor(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

Oct 15 '07 #2
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(co nst 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(co nst 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.

Oct 15 '07 #3
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.

Jim Langston wrote:
"Hank stalica" <me@nospam.netw rote in message
news:69******** *************** *******@comcast .com...
>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(c onst Floor& f)
:destFloor(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

Oct 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16248
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
74
43273
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
11
2380
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
18
2242
by: DaveLessnau | last post by:
I'm trying to learn C on my own and, apparently, my brain went on vacation somewhere. I just can't figure out how to parse the following function call: "void fillDeck( Card * const wDeck, const char * wFace, const char * wSuit)" Card is aliased before that call with: "typedef struct card Card"
20
2478
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
10
2796
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
14
6659
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows it. Specifically, part 1 is obvious to most anyone. Part 2 is isomorphic to part 1, yet behaves differently. If a shared_ptr is const, should it really allow non-const dereferences? Thanks,
14
1991
by: Wolfgang | last post by:
As I understand, a const member function is used by const object to ensure that its instance isn't modified throughout its life. Am I missing something.. #include <iostream> using namespace std; class A{ public:
10
5977
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it is unused, I know that it does not take up storage in the
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.