I just wanted to hide the overridden methods since they are no longer needed in the public section. And I wasn't going to use a base class pointer.
But out of interest, I tried to call a method moved to a private section using a pointer to the base class. And it worked...
#property strict class CBase { public: virtual void virtualMethod() { Alert("Base"); }; }; class CChild final : public CBase { private: virtual void virtualMethod() override { Alert("Child"); }; }; void OnStart() { CBase *a = new CChild; a.virtualMethod(); // Child delete a; }
And it also confuses me that the compiler normally perceives the use of the virtual keyword in a private section. This doesn’t make any sense (as far as I know) because a private method cannot be overridden.
But that's just my interest. First of all, I'm interested in this:
Can I change the access to a base class method by using a different access specifier when overriding the method in a descendant?
I want to make sure this is normal practice.
[EDITED]
I just saw an extra semicolon after the body of methods in my code. It's a typo
And it also confuses me that the compiler normally perceives the use of the virtual keyword in a private section. This doesn’t make any sense (as far as I know) because a private method cannot be overridden.
But that's just my interest. First of all, I'm interested in this:
[EDITED]
I just saw an extra semicolon after the body of methods in my code. It's a typo
Of course a private method can be overridden.
Yes you can use a different access specifier. It's normal practice.

- 2010.01.26
- hlx236sk hlx236sk 353 1 1 gold badge 3 3 silver badges 4 4 bronze badges
- stackoverflow.com
Of course a private method can be overridden.
It turns out that the virtual specifier makes the method available for overriding in descendants, regardless of the access specifier in the base class.
And from this it follows that declaring a PURE method in the private section is meaningless.
Thank you very much, Alain
It turns out that the virtual specifier makes the method available for overriding in descendants, regardless of the access specifier in the base class.
And from this it follows that declaring a PURE method in the private section is meaningless.
Why meaningless ?
It's meaningful on the contrary. If it's an implementation detail it should better be private.
Why meaningless ?
It's meaningful on the contrary. If it's an implementation detail it should better be private.
This is another typo, sorry. Looks like I should sleep more.
I meant to write the word "protected":
And from this it follows that declaring a PURE method in the private protected section is meaningless.
- virtual makes a method available for overriding
- PURE method, should be overridden
If the protected keyword is used, hidden data can be accessed also from methods of classes - inheritors of this class.
Therefore, I concluded that placing a pure virtual function in a protected section is pointless
Therefore, I concluded that placing a pure virtual function in a protected section is pointless
Unless you plan for the child to call a method that is not yet implemented (will be implemented by the child's child)...
But personally, I would look for a way to avoid such complications😄
Unless you plan for the child to call a method that is not yet implemented (will be implemented by the child's child)...
But personally, I would look for a way to avoid such complications😄
I'm not sure if I understood you correctly.
Here is my attempt to make code according to your example:
class CBase { private: virtual int search() = 0; // No need to place PURE function in protected section public: void add() { //... Alert(search()); // Use of search() method //... } }; class CChild_1 : public CBase { private: int search() override { return(111); } }; class CChild_2 : public CBase { private: int search() override { return(222); } }; void OnStart() { CBase *child_1 = new CChild_1; CBase *child_2 = new CChild_2; child_1.add(); // 111 child_2.add(); // 222 delete child_1; delete child_2; }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Can I change the access to a base class method by using a different access specifier when overriding the method in a descendant?
I want to make sure this is normal practice. I haven't seen similar examples in the documentation, and I don't know any other programming languages