i have two members. First one: m_a is a constant reference to pointer of int.
The next one: m_b is an int pointer.

In the constructor i've initialized the members as shown below.

Code:

TestClass::TestClass()
:m_b(0),
m_a(m_b)
{
}

Then i built it in VC++ 6.0, No errors shown, everything was fine.

Then I opened the same application in VC++ 9.0 (VS2008), It is showing a compilation error as follows.
1>d:\technical\TestClass\TestClass.cpp(66) : error C2440: 'initializing' : cannot convert from 'int *' to 'const int *&'
1> Conversion loses qualifiers
1>d:\technical\TestClass\TestClass.cpp(66) : error C2439: 'TestClass::m_a' : member could not be initialized
1> d:\technical\TestClass\TestClass.h(55) : see declaration of 'TestClass::m_a'

Why this error happens? Is there any way to solve this error? Please help....

February 5th, 2009, 06:53 AM

cilu

Re: Initialization of constant pointer reference member variable

Because VS2008 has a better compiler than VC++ 6.0.

Why are you trying to do with that piece of code?

February 5th, 2009, 08:16 AM

rageshc

Re: Initialization of constant pointer reference member variable

Thanks a lot for your reply.

Actually i was trying to compile a huge project done in VC6 to VC9.
So i came in a similar situation.

If it shows error in previous case, in this case also it should show an error right?

If possible, please mention the reason for getting no errors here.

February 5th, 2009, 08:42 AM

Lindley

Re: Initialization of constant pointer reference member variable

The basic problem is that a reference to a const int* cannot be pointed at an int*.

If you meant for the *reference* to be const, then this might work (I'm not really sure....)

Code:

ABC* const & m_a;

February 5th, 2009, 09:23 AM

rageshc

Re: Initialization of constant pointer reference member variable

Quote:

Originally Posted by Lindley

The basic problem is that a reference to a const int* cannot be pointed at an int*.

If you meant for the *reference* to be const, then this might work (I'm not really sure....)

Code:

ABC* const & m_a;

I think you have answered my question. i tried it. it compiled and no error is shown. I think it works. I'll update soon after getting more information about it.
Thank you very much for your reply.

February 5th, 2009, 10:14 AM

Codeplug

Re: Initialization of constant pointer reference member variable

The reasoning is a little awkward to comes to grips with. The main question is:Since a "const int&" can be bound to an "int", why can't a "const int*&" be bound to a "int*"?

Basically, once you add a level of indirection (a pointer) then the rules change. With just a single level of indirection (as in a single *), the rule can be stated as:

A reference to a pointer to a cv-qualified type can be bound to anything of that same type whose cv-qualifications are less than or equal to that of the pointer (which is a reference). (Read that a few times.)

So the reason a "const int*&" can't be bound to a "int*" is because "const int*" and "int*" are two different types (underlined part of the rule is broken).

What you can bind (as Lindley suggested) is an "int*const&" to an "int*". Here, the reference can be bound to anything of the same type ("int*" in this case) as long as the cv-qualification is less than or equal to "const".

I've tried to put a rule into words for a single level of indirection. Section 4.4-4 of the standard tells you how to handle cases where there are one or more levels of indirection.
The reasoning behind treating pointers differently is related to this FAQ entry: http://www.parashift.com/c++-faq-lit...html#faq-18.17

>> If you meant for the *reference* to be const ...
It is more natural to say "the reference is constant". But technically, it's the pointer that is const, as apposed to what it points to. References are const by default - since once they're bound, you can't re-bind them ;)

This means that "m_a" cannot be changed, but "m_a->member" can be changed - which is different from what VC++6.0 (allowed to be) compiled.