Non overloadable function

Hi there!

Please I would like to know if there is a way to make a function non overloadable.
My idea is to keep it private in the class so that it will be impossible to overload the function in a derived class.

But if in the base class (the one that holds the private function) I also have a public or protected function (let's call it plf) to manage the private one (let's call it prf), and in the derived class I create a function with the same name as the private base class function (prf) that calls for the public base class function (plf), I am wondering if there won't be a conflict between both of the same-named (prf) functions?

Assuming the prf function of the derived class is not an overload of the prf function of the base class (because the last one is not visible).

Thanks in advance.
If you don't need to use the base class function in your derived class, and you want to have a function of the same name in your derived class, you should make the base class function a Pure Virtual function.
You're right. But the purpose here is that the private base class function is a kind of background function. It is useful but I don't want anyone to access it directly (through public access or inheritance for protected access). that is why I think of a public function in the base class to get parameters from any functions in the derived class that the private base class function will process. It's an interface for some operations I want to keep safe from other users.

My concern is that I would like to prevent any user from creating an overload of that interface.

thanks!
I think I've made a mistake.

My concern is more a matter of overriding not overloading. I don't want the function to be overridden in derived classes. That is why I thought of keeping it private.

Sorry!
I just did a quick test with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class a
{
    private:
        void test ( );
};

class b : private a
{
    public:
        void test ( );
};

int main ( )
{
    b var;

    var.test ( );
}

In this case, class b's test() will be called. If class b did not have a test(), it would attempt to call class a's test, but it can't because it's private.
Last edited on
Thanks!

Here is a quick test I've made too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class a
{
private:
    void x();
public:
    void y();
    a();
};

class b : public a
{
public:
    void x();
    b();
};

a::a(){}

void a::x()
{
    cout << "Write a" << endl;
}

void a::y()
{
    x();
}

b::b(){}

void b::x()
{
    y();
}

int main()
{
    b t;
    t.x();
    return 0;
}


This returns a's x() without any conflict.

Looking around the web I also note that C++ now uses the final keyword as Java (not the same way I think.) and that may also help. I have to take a look at it.

Thanks a lot.
Topic archived. No new replies allowed.