如果使用引用传递对象,则会增加引用计数类中的引用计数
should passing an object using its reference increase the reference count in a reference counting class
我正在编写一个使用引用计数来管理其资源的类。
如果类使用其引用传递给函数,是否应该添加引用?
如果我的对象通过它的引用传递给一个函数,我会在调用的类上获得一个复制构造函数或任何其他方法吗?
例如,我想知道它是如何在share_pointer或cv::Mat中实现的。
如果我将cv::Mat传递到使用引用的方法中,是否添加了它们的引用计数器?
例如:
void func(cv::Mat & image)
{
// what is the reference counter here? is it one or two?
}
main()
{
cv::Mat image;
// reference counter for image should be one here.
funct(image);
}
引用计数通常在复制构造函数、赋值运算符和析构函数中跟踪。如果通过引用传递,那么这些函数都不会被调用,因此引用计数将保持不变。
void func(cv::Mat image) //reference count increased due to copy
{
} //reference count decreased due to destruction
void func(cv::Mat& image) //no copy, reference count unchanged
{
} //no destructor call, reference count unchanged
通常,当一个类有引用计数时,它会存储该对象的副本数量。通过引用传递对象时,不会进行复制。编译器可能正在使用指针,也可能正在使用实际对象,但它没有进行复制,因此引用计数器不会增加。
相关文章:
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- 为什么我的代码在输出中增加了93天
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 作为赋值(增加引用变量)C++的左操作数所需的左值
- C++为什么原始指针不会增加shared_ptr的引用计数?
- 从工厂方法返回的ComPtr的引用计数增加两次
- 如何引用其他类中的函数以向量增加价值
- shared_ptr:在基类的shared_ptr中复制时,引用计数是否增加
- 在不增加引用计数的情况下迭代共享指针的映射
- 如果使用引用传递对象,则会增加引用计数类中的引用计数
- shared_ptr增加父引用
- IDispatch::Invoke(DISPATCH_PROPERTYGET)是否增加返回的IDispatch接口的引用
- 我可以使用cmpxchg16b自动地将指针复制到指针和int,同时增加int(原子引用计数)
- 添加对共享ptr的引用会增加引用计数吗?