- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有课,DevicePointer
,它封装了一个 std::shared_ptr<Device>
.需要保存指向设备的指针的各种类派生自 DevicePointer
.在我开始使用之前 shared_ptr
, DevicePointer
有一个::Expose()
将返回指向设备的原始指针的函数。现在我正在使用 shared_ptr 来保存设备指针,我不确定如何返回它。请注意,唯一的原因是 ::Expose
应该调用的是取消引用指针。
这是原始 Expose 的样子:
Device * Expose() const { return MyDevice; }
并且会像这样使用:
Device::Expose()->ExecuteFunction(a, b, c);
现在MyDevice
是 std::shared_ptr<Device>
,我不确定如何返回它以取消引用。显而易见的选择是:
std::shared_ptr<Device> Expose() {
return MyDevice;
}
但我担心性能,尤其是新临时文件的创建 std::shared_ptr
.所以我需要某种方式来表达“你可以取消引用这个指针,但你不能复制它”。原始文件仍然需要共享,因为许多对象将持有对它的引用。
我希望我已经充分阐明了我的问题。谢谢。
最佳答案
这不会影响性能
Device & Expose() {
return *MyDevice.get();
}
编辑:
使对象不可复制:
class Device
{
private:
//compiler will throw errors when copy constructor or = is called in the code
Device(const Device &)
{}
void operator = (const Device &)
{
}
};
编辑(2018-09-05):
从 c++11 开始,您可以显式删除复制构造函数或赋值运算符:
class Device
{
public:
Device(const Device &) = delete;
Device& operator = (const Device &) = delete;
};
关于c++ - 取消引用从函数返回的 shared_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19794803/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!