::开放是什么意思?
我需要帮助了解C ++语法。我正在引用derek molloy github ,
/chp08/i2c/cpp/i2cdevice.cpp
在其实施文件中,他在他的代码中有这一部分,
int I2CDevice::open(){
string name;
if(this->bus==0) name = BBB_I2C_0;
else name = BBB_I2C_1;
if((this->file=::open(name.c_str(), O_RDWR)) < 0){
perror("I2C: failed to open the bus\n");
return 1;
}
if(ioctl(this->file, I2C_SLAVE, this->device) < 0){
perror("I2C: Failed to connect to the device\n");
return 1;
}
return 0;
}
我对此特定行感到困惑, if(((this-&gt; file = :: open(name.c_str(),o_rdwr),o_rdwr))&lt; 0)
。 = :: Open
到底是什么意思?我知道C ++中的FSTREAM库有一个打开的方法,但是为什么包括::
?
I need help understanding C++ syntax. I am referencing Derek Molloy Github,
/Chp08/i2c/cpp/I2CDevice.cpp
In his implementation file, he has this section in his code
int I2CDevice::open(){
string name;
if(this->bus==0) name = BBB_I2C_0;
else name = BBB_I2C_1;
if((this->file=::open(name.c_str(), O_RDWR)) < 0){
perror("I2C: failed to open the bus\n");
return 1;
}
if(ioctl(this->file, I2C_SLAVE, this->device) < 0){
perror("I2C: Failed to connect to the device\n");
return 1;
}
return 0;
}
I am confused on this particular line,if((this->file=::open(name.c_str(), O_RDWR)) < 0)
. What exactly does =::open
mean? I know the fstream library in C++ has an open method, but why include the ::
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
name :: func()
表示在命名空间名称
下调用func
。在没有名称的全局名称空间中定义了一些诸如Open
之类的功能,因此:: Open
调用来自Global Namespace的函数open
。它用于避免调用i2cdevice :: Open
,并明确调用操作系统的open
函数。name::func()
means to callingfunc
under the namespacename
. Some functions likeopen
are defined in the global namespace which has no name, so::open
calls a function namedopen
from the global namespace. It is used here to avoid callingI2CDevice::open
and to explicitly call the operating system'sopen
function.进行函数调用时,您可以将函数的命名空间前缀为:这
通常是这样的:
但是全局名称空间中的事物可以通过较短的
::
访问,因此这是一种方式当编译器可能从本地范围中选择一个函数时,指定特定函数。
在您的情况下,您可能需要使用
:: open()
,因为您的类具有函数i2cdevice :: open()
,并且编译器将尝试使用一个(和生成错误)。但是,通过使用:: Open()
特定使用,您说的是使用全局名称空间中的open()
的版本。When doing a function call you can prefix the function with the namespace it is in:
This usually looks like this:
But things in the global namespace, can be accessed by the shorter
::
So this is a way of specifying a specific function when the compiler may have picked a function from a local scope instead.
In your case you probably need to use
::open()
as your class has functionI2CDevice::open()
and the compiler would have tried to use that one (and generated an error). But by being specific with::open()
you are saying use the version ofopen()
that is in the global namespace.