Re: How do I make the members of a parent class visible without using the "this" pointer?
peteymills@hotmail.com schrieb:
It used to be that all the members (except those declared private) of
a parent class were visible to its descendants as if they had been
declared within the descendant class itself. Now some brain-surgeon
has changed the standard so that you have to add the "this" pointer,
requiring a load of extra, redundant typing for C++ code that uses
inheritance.
When, were, why? Answer: No, it's not.
class A {
protected:
int x;
};
class B : public A {
public:
void setX(int n)
{
x = n;
}
};
int main(int argc,char **argv)
{
B b;
return 0;
}
Works as it should.
Do you mean, with templates involved?
template<typename T> class A {
protected:
T x;
};
template<typename T> class B : public A<T> {
public:
void setX(T n)
{
x = n; // (1)
}
};
int main(int argc,char **argv)
{
B<int> b;
b.setX(0);
return 0;
}
This shouldn't compile, because x is not a dependent name, and at the
point (1), two-phase lookup requires the compiler to locate x as
non-template dependent name. *One* way to deal with this is through a
"this" pointer:
template<typename T> class B : public A<T> {
public:
void setX(T n)
{
this->x = n;
}
};
which makes x dependent. Alternatives to make x depending:
template<typename T> class B : public A<T> {
public:
void setX(T n)
{
A<T>::x = n;
}
};
or:
template<typename T> class B : public A<T> {
using A<T>::x;
public:
void setX(T n)
{
x = n;
}
};
Why is this so? Consider partial specialization:
template<typename T> class A {
};
template<> class A<int> {
protected:
int x;
};
int x;
template<typename T> class B : public A<T> {
public:
void setX(T n)
{
x = n; // (2)
}
};
Woops! At (2), which x has the compiler to pick? Should this mean, the x
is the x from A if T is an int, and the global x otherwise? This is
pretty surprising and error-prone! To this end, two-phase lookup
requires that at (2), the x is as independent name resolved to the
global x, *always*, independent of T (as it is not dependent).
This is fine if you are starting from scratch, but what
if you have legacy code that needs to be compiled and you don't feel
like picking through the code to add the "this" pointer to every
single inherited member?
Please feel ensured that nobody changed the standard, just that your
code worked by accident due to a bug in older g++ releases.
Is there any way I can tell g++ to use the
old rules (without reverting to an older version)?
G++ had a transitional compiler switch for quite a while to incorrectly
accept this, which generated a warning, before it supported two-phase
lookup properly. However, it shouldn't have compiled the code in first
place, and for at least one revision this warning existed, giving you
quite a while to fix your code. I'm currently not aware of this option,
but I seem to remember that it has been phased out in recent releases.
So long,
Thomas
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]