- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 wxWidgets,如果您曾经使用过它,您就会知道基类中有很多 公共(public)函数。我最近遇到了一种情况,我不希望直接从派生类调用方法 SetText()
。也就是说,派生类继承了 SetText()
函数,但我不希望客户端可以使用此函数。相反,我提供了两个调用 SetText()
的新函数,但在执行一些额外操作之前不会调用。
目前,客户端(我!)可以忘记调用特殊函数而只需调用 SetText()
。因此,将不会执行一些额外的操作。这些操作非常微妙,很容易被忽视。
那么,我能否将个别函数标记为私有(private)函数,以便客户端无法调用它们,或者只是让客户端无法直接调用它(他们将不得不使用我的函数来间接调用它)?
请注意 SetText()
不是虚拟的。
编辑:对于偶然发现这个问题的 future 程序员,请检查标记的答案和 Doug T.'s回答。
最佳答案
实际上有两种方法可以做到这一点。 Doug T. 很好地概述了第一个 - 使用私有(private)/ protected 继承和组合 - 所以我不会深入讨论那个。
使用私有(private)/ protected 继承的问题是它掩盖了一切。然后,您必须有选择地公开您仍想公开的成员。如果您希望大部分内容都保持公开状态,并且只想掩盖一件事情,那么这可能会让人头疼。这就产生了对第二种方法的需求 - 使用 using
关键字。
例如,在您的情况下,您可以简单地声明您的类如下:
class Child : public Parent
{
//...
private:
using Parent::SetText; // overrides the access!
};
这仅屏蔽了 SetText 方法!
请记住,指向 Child
的指针始终可以转换为指向 Parent
的指针,并且可以再次访问该方法 - 但这是一个问题继承也是:
class Parent
{
public:
void SomeMethod() { }
void AnotherMethod() { }
};
class ChildUsing : public Parent
{
private:
using Parent::SomeMethod;
};
class ChildPrivateInheritance : private Parent
{
};
void main()
{
Parent *p = new Parent();
ChildUsing *a = new ChildUsing();
ChildPrivateInheritance *b = new ChildPrivateInheritance();
p->SomeMethod(); // Works just fine
a->SomeMethod(); // !! Won't compile !!
a->AnotherMethod(); // Works just fine
((Parent*)a)->SomeMethod(); // Compiles without a problem
b->SomeMethod(); // !! Won't compile !!
b->AnotherMethod(); // !! Won't compile !!
((Parent*)b)->SomeMethod(); // Again, compiles fine
delete p; delete a; delete b;
}
尝试访问 ChildUsing
实例上的 SomeMethod
会产生(在 VS2005 中):
error C2248: 'ChildUsing::SomeMethod' : cannot access private member declared in class 'ChildUsing'
但是,尝试在 ChildPrivateInheritance
实例上访问 SomeMethod
或 AnotherMethod
会产生:
error C2247: 'Parent::SomeMethod' not accessible because 'ChildPrivateInheritance' uses 'private' to inherit from 'Parent'
关于c++ - 我可以将基类的个别成员的状态更改为私有(private)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7182290/
如果需要在类外访问静态(例如单例),可以选择公共(public)静态而不是私有(private)静态,而当不需要公开函数时首选私有(private)静态(否则未命名的命名空间就可以了)——在这种情况下
在互联网上进行了一些搜索,但找不到简单的答案。我的问题集是在 Android 框架中使用 Java,但我相信这也是标准的 Java 行为。我理解 final 和 private 的定义,它们都用于变量
我有这个代码: public final class Board { private final int[][] blocks; private final int N; pr
对我来说,过去作为 Objective-C 开发人员很简单。一个类需要公开的每个字段都是一个属性,每个私有(private)字段都是一个没有 getter 或 setter 的实例变量。但我经常看到人
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个在 Docker 容器中运行的应用程序。它需要来自公司私有(private) NPM 注册表(Sinopia)的一些私有(private)模块,并且访问这些需要用户身份验证。 Dockerfi
我试图理解 C# 使用 getters 和 setters 自动声明变量与 java 声明之间的区别。 在java中我通常这样做: private int test; public int getTe
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我目前正在使用 Objective-C(适用于 iPhone)构建游戏。 为此,出于性能/复杂性原因,我略微打破了 MVC,并为 View (渲染器)提供了对模型的直接引用。这是因为它应该以 60fp
我已经在 ubuntu 上成功配置了 2 个虚拟主机站点(基于名称的虚拟主机)。我的 apache 版本是 2.2.22。 这两个站点都在本地主机上工作。 /etc/hosts 条目 127.0.0.
考虑下面的类 public class A { private final Map cache; public HeavyObject getThing(); } 假设不能泄漏对缓存
我有一个类,它有一个方法,我希望它只能被它的子对象访问,而不能被这个包中的其他类访问。 Modifier | Class | Package | Subclass | World ———————
本文实例讲述了JavaScript中的公有、私有、特权和静态成员用法。分享给大家供大家参考。具体分析如下: 下面的内容是在《JavaScript.DOM高级程序设计》里面摘抄出来的,比较容易理解,
我有一个用例,我已将其简化为以下程序: public class A { private int x = 100; class B { private int y = ne
问题: 类声明如下: class Select { public: template static Iterator function(Iterator , Iterator , bo
我是一名初级 PHP 程序员。我还有很多东西要学。这就是我问这个问题的原因。在一个类中,您有一个公共(public)函数,您可以从该类外部调用它。有时你有一个私有(private)函数,你可以在私有(
问题是: 何时使用私有(private)函数,何时使用嵌套函数? (我在问 F# 但也许答案可能与其他功能语言相关) 一个小例子 namespace SomeName module BinaryRea
我发现工作表中仍然可以使用私有(private)函数。它们是隐藏的,但如果用户输入他们的名字,他们就会被调用。为什么?它应该以这种方式工作吗?有没有办法完全阻止用户定义的函数在 VBA 项目之外使用?
所以我最近开始尝试使用 Kotlin,我偶然发现了这个: If a top-level declaration is marked private, it is private to the pack
我是一名优秀的程序员,十分优秀!