Hiding members using `using'

P

Paul Bibbings

This is just an observation (of my own ignorance, really). I have many
times used a class-scope using declaration to `promote' the
accessibility of a member of a base class in a derived class, but I
hadn't ever applied it to do the opposite, i.e. hide a base class
member. Consequently, though I had no reason to suppose that it wasn't
possible, I did have to try it out (`engineer's solution' - no access to
the Standard at this moment) and found that it does work.

#include <iostream>

class A {
private:
int i_;
public:
A(int i) : i_(i) { }
void seti(int i) { i_ = i; }
int geti() const { return i_; }
};

class B : public A {
public:
B(int i) : A(i) { }
void dbli() { seti(2 * geti()); }
private:
using A::seti; // hide
};

int main()
{
A a1(1);
a1.seti(2);
std::cout << "a1.geti() = " << a1.geti() << '\n';
B b1(3);
// b1.seti(4); // inaccessible
b1.dbli();
std::cout << "b1.geti() = " << b1.geti() << '\n';
}

/**
* Output: // largely irrelevant
* a1.geti() = 2
* b1.geti() = 6
*/

So... Yay!

Regards

Paul Bibbings
 
A

Alf P. Steinbach

* Paul Bibbings, on 05.06.2010 10:31:
This is just an observation (of my own ignorance, really). I have many
times used a class-scope using declaration to `promote' the
accessibility of a member of a base class in a derived class, but I
hadn't ever applied it to do the opposite, i.e. hide a base class
member. Consequently, though I had no reason to suppose that it wasn't
possible, I did have to try it out (`engineer's solution' - no access to
the Standard at this moment) and found that it does work.

#include<iostream>

class A {
private:
int i_;
public:
A(int i) : i_(i) { }
void seti(int i) { i_ = i; }
int geti() const { return i_; }
};

class B : public A {
public:
B(int i) : A(i) { }
void dbli() { seti(2 * geti()); }
private:
using A::seti; // hide
};

int main()
{
A a1(1);
a1.seti(2);
std::cout<< "a1.geti() = "<< a1.geti()<< '\n';
B b1(3);
// b1.seti(4); // inaccessible
b1.dbli();
std::cout<< "b1.geti() = "<< b1.geti()<< '\n';
}

/**
* Output: // largely irrelevant
* a1.geti() = 2
* b1.geti() = 6
*/

So... Yay!

Someone asked here about how to do this very recently. The natural
counter-question is why would one want to do that? E.g. it's easy to circumvent
by using a reference to base.

But then, just about every C++ restriction can be circumvented.

Sign that one is getting older: before 'using' one achived the same by sort of
re-declaring the base attributes, but I don't remember the syntax. Did it
involve qualification with the class name? And speaking of that, is it valid to
qualify a declaration of an attribute of the class itself?


Cheers,

- Alf
 
P

Paul Bibbings

Alf P. Steinbach said:
* Paul Bibbings, on 05.06.2010 10:31:

Someone asked here about how to do this very recently.

Leigh had suggested something along these lines, and it was this thought
that triggered my experimenting. In his idea, IIRC, Derived extended
Base by private inheritance and then the wanted interface was brought
back in using `using'. It looked a little round-abouts, which got me to
thinking about the possibility of doing it as above.
The natural
counter-question is why would one want to do that? E.g. it's easy to
circumvent by using a reference to base.

I have to be honest and agree with you. I can't, off hand, think of a
use case in which it might prove, well, useful.
But then, just about every C++ restriction can be circumvented.

Sign that one is getting older: before 'using' one achived the same by
sort of re-declaring the base attributes, but I don't remember the
syntax. Did it involve qualification with the class name? And speaking
of that, is it valid to qualify a declaration of an attribute of the
class itself?

I'm not going to embarass you and start comparing ages but, was this
considerably pre-c++98? I don't remember it myself, although I'm am
probably old enough to have done so, had I not come to C++ for the first
time right around '98/'99.

Regards

Paul Bibbings
 
J

Juha Nieminen

Alf P. Steinbach said:
But then, just about every C++ restriction can be circumvented.

That may be true, but the relevant question is how easy is it to
circumvent a restriction accidentally. That's the key. When you use
properly the tools offered by the language to prevent accidental misuse,
then your program becomes more robust.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top