我正在编写一个使用引用计数来管理其资源的类。

如果该类使用其引用传递给函数,是否应该添加引用?

如果我的对象通过其引用传递给函数,我是否在类上得到了复制构造函数或任何其他方法?

例如,我想知道如何在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++ - 应该使用其引用传递对象,从而增加引用计数类中的引用计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36336899/

10-11 22:45
查看更多