-
February 5th, 2009, 05:51 AM
#1
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 07:27 AM.
-
February 5th, 2009, 07:53 AM
#2
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, 09:16 AM
#3
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.
It was actually a structure instead of int.
The code was some thing like the following...
Code:
struct ABC
{
...
...
}
struct BCD
{
ABC* ptr;
}
class CTestClass
{
CTestClass();
const ABC*& m_a;
BCD* m_b;
}
CTestClass::CTestClass()
:m_b( /*some initialization is done here*/ ),
m_a( m_b.ptr )
{
}
Is that usage really illegal?
What is the problem in it?
It is also for your information that the following code compiled in VC9 without any errors.
Code:
typedef const int* INT_CP;
class CTestClass
{
CTestClass();
INT_CP& m_a;
INT_CP m_b;
}
CTestClass::CTestClass()
:m_b(0),
m_a(m_b)
{
}
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.
Last edited by rageshc; February 5th, 2009 at 09:23 AM.
-
February 5th, 2009, 09:42 AM
#4
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....)
-
February 5th, 2009, 10:23 AM
#5
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....)
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
-
February 5th, 2009, 11:14 AM
#6
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.
gg
-
February 5th, 2009, 11:50 PM
#7
Re: Initialization of constant pointer reference member variable
Hi gg,
That was a highly informative post.
Thank you very much
Regards,
Ragesh C
If you win, you need not explain. But if you lose,
you should not be there to explain. - Adolf Hitler
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|