disable access to some functions of parent class

hello,

Briefly I want to disable to access some functions of the parent class from child class

For example. CreateChild() and CreateChild(String name) these functions should never be accessed from TSNode class, I don't want to inherit as private or protected, I just want some functions of the parent to be invisible to the child.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class Node {
public:
CreateChild();
CreateChild(String name);

}

class TSNode: public Node {
public:
CreateChild(String name, Vector3 position);

}
I'm fairly new to C++, but did you try making those functions private? I think that way you can restrict access.
good thinking thanx :)
marking them as private worked just fine ^^

1
2
3
4
5
6
7
8
9
class TSNode : public Node {
private:

CreateChild();
CreateChild(String name);

public:
CreateChild(String name, Vector3 position);
}
I figured out that, this is not an actual solution :\ these functions are accessible through polymorphism

1
2
3
4

Node* n = new TSNode();
n->CreateChild(); // No error :\



Last edited on
Oh, same function name in the parent and child classes huh! Is it inevitable that they all should have the same function name? If not you can remove the polymorphism by changing the name of the function. If it important that the parent and the child classes have the same name function then use dynamic binding by declaring the function in the parent class definition 'virtual'. Hope this helps.
1
2
Node* n = new TSNode();
n->CreateChild(); // No error :\ 

This polymorphism won't work as expected.

You define functions with the same name in both base and derived class but you don't declare them as virtuals. This menas that the compiler will consider them to be their static type (the type their pointer is, i.e. here Node!). You can't see CreateChild() of the derived class this way. Use virtual function in order to provide the compiler the ability to choose which class the objects belong to (base or derived one).

And by defining them as private the are indeed hidden from derived class objects
1
2
Node* n = new TSNode();
n->CreateChild(); // No error :\ 


A Node* will always allow what is legal for a Node to be called.

If you want to stop callers from accessing certain functions if a Node* is actually pointing to a subclass then you could overrride the functions in the subclass and make them throw an error. However I would seriously question the design here. The whole point of inheriting from a Node is that the subclass is everything that a Node is and then some. Usually the caller should not be able to tell or even care what subclass is *actually* being pointed to.

Topic archived. No new replies allowed.