The milky way -> Earth -> Asia -> India -> Kerala -> Thrissur -> Eravu :)
Posts
4
Initialization of constant pointer reference member variable
I've created an application in VC++ 6.0. Pls see the code below.
Code:
class TestClass{
public:
TestClass(); // standard constructor
const int *& m_a;
int* m_b;
};
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....
Last edited by rageshc; February 5th, 2009 at 06:27 AM.
The milky way -> Earth -> Asia -> India -> Kerala -> Thrissur -> Eravu :)
Posts
4
Re: Initialization of constant pointer reference member variable
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.
Regards,
Ragesh C If you win, you need not explain. But if you lose,
you should not be there to explain. - Adolf Hitler
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.
Bookmarks