- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个类:
class Impl1 : public POA_I1
{
private :
Impl2_var ob;
public :
Impl2_ptr get();
{
return ob;
}
void set(::Impl2_ptr ob)
{
this->ob = ob;
}
};
我对 _ptr
和 _var
有点困惑。我读过
MyObject_var The _var version of the object reference type acts as a handle toproxy in much the same way as a _ptr reference but also adds memory management. Like all _var types, a _var reference takes care of deallocating its underlying instance (in this case, the proxy instance) when the reference goes out of scope. Both _ptr references and _var references allow the client to access operations onproxy instance
但我不明白什么时候使用每一个以及为什么。我的意思是,在我的实现中 = 我应该使用哪个,_var
或 _ptr
为什么?在类中有一个 _var
类型的字段并在 setter 中返回 _ptr
是否合法?我根本不明白。
最佳答案
正如 Johnny 所指出的,这全都与内存所有权有关。如果将 _ptr 变量分配给 _var 变量,当 _var 变量超出范围时,内存将被删除,您最好不要使用该 _ptr 变量。
在您提供的情况下,当您调用 set
时,您将指针的所有权授予 Impl1
类型的对象。你仍然可以使用你拥有的那个,你可以调用 Impl1::get
但是因为你通过调用 set
给了那个 _ptr 的对象所有权,如果对象被删除所以执行该指针引用的内存。
关于c++ - _ptr 或 _var - 哪个用作类字段,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11938070/
我发现自己不确定是否应该将我在我的类中使用的某些变量设置为私有(private)类变量(例如 _var)与类常量变量(例如 VAR)与私有(private)类常量变量,如果这样的话使用(例如 _VAR
我正在尝试学习 C++(目前只知道 PHP 和一些 C#)并且遇到了我的第一个问题。 我试图在开关内调用一个类,然后在开关后使用该定义的变量。但是,我收到标题中描述的错误。 #include #in
这个问题在这里已经有了答案: What's the deal with a leading underscore in PHP class methods? (14 个答案) 关闭 9 年前。 PH
我最近更新到 PHP 5.3.6,其中有一些已弃用的函数和 INI 指令。我有以下功能: function ServerVar($varName) { global $HTTP_SERVER_VARS
我最近更新到 PHP 5.3.6,其中有一些已弃用的函数和 INI 指令。我有以下功能: function ServerVar($varName) { global $HTTP_SERVER_VARS
随着所有这些新的 ARC 内容(不属于 NDA...)的出现,处理属性的默认设置似乎是在实现文件中显式设置没有和 ivar 的属性,直到您将它与类似的东西合成: @synthesize var = _
我有一个类: class Impl1 : public POA_I1 { private : Impl2_var ob; public : Impl
鉴于此: fn main() { let variable = [0; 15]; } Rust 编译器产生这个警告: = note: #[warn(unused_variables)] on b
使用 _ 命名任何未使用的变量有一个通用规则。在 Elixir 中。这样做会阻止任何绑定(bind)到该变量的东西。 但是,我注意到一种广泛使用的模式,即使用下划线前缀来表示忽略的参数,形式为 _ta
我很好奇地寻找为什么在 C#.NET 设计器中不允许使用以下代码的解释: const foo f = new foo(); 它给出了以下错误信息: 'f' is of type 'ConsoleApp
这段代码: $query = array( "var" => "no", "not_var" => "yes", "var2" => "maybe" ); print http
我是一名优秀的程序员,十分优秀!