Hiding functionality of members in a base class

Hello,

In inheritance, you can hide or change the functionality of members in a base class. Usually, you either overwrite a member function/variable with the same name but different contents in the derived class, but also, you are allowed to change the access specifier in the derived class.

The latter is something that confuses me a little bit. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
 
class X
{
protected:
    void func1() { cout << "Hi\n"; }
};
 
class Y : public X
{
public:
    X::func1;
};
 
int main()
{
    return 0;
}


It seems that I cannot do the same for namespaces:

http://ideone.com/qQ3SS

So is this just a unique feature for inheritance?
Well, you can. You just need to define a new name in the other namespace; not define a new namespace and then access the old one anyway!

Namespaces don't work like classes, they're just ways of avoiding clashing names by wrapping everything under one name, or more than one name.
@Veltas,

Mind explaining more, maybe with an example?

Or is this just something unique with derived classes/inheritance?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
 
namespace X
{
        void func1() { cout << "Hi\n"; }
}
 
namespace Y
{
        //X::func1;   func1 already exists in the namespace X!  Doesn't work.
        void func1(); // But here we're not using the X namespace, so we're creating a new func1 in the Y namespace.
}
 
int main()
{
        X::func1();
        Y::func1();
        return 0;
}
What I am trying to do is create the same function (func1) in X and have it in Y as well. You can do this in inheritance because a derived class can change the access specifier of base class members.
I'm not sure what you want makes sense considering the idea behind a namespace. What about the below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
namespace X
{
        namespace Y
        {
                void func1() { cout << "Hi\n"; }
        }
}
 
int main()
{
        X::Y::func1();
        return 0;
}
I guess that was a bad comparison to start with. I was just wondering if copying other class's members are only in a child to parent class relationship.
EDIT: I have no idea what I'm doing, maybe this compiles. Really though, yes if it's inheritance you want make a class!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
 
namespace X
{
        void func1() { cout << "Hi\n"; }
        namespace Y
        {
        }
}
 
int main()
{
        X::func1();
        namespace Y
        {
                func1();
        }
        return 0;
}
Last edited on
This could be useful in other situations, possibly. Too bad it's only for derived classes.
We could use any feature in any way we want, but the features apply to appropriate situations only to avoid us abusing them. And honestly I'd feel like I was abusing namespaces if I attempted what you asked about.
Topic archived. No new replies allowed.