::开放是什么意思?

发布于 2025-02-09 14:48:25 字数 849 浏览 2 评论 0原文

我需要帮助了解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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野侃 2025-02-16 14:48:26

name :: func()表示在命名空间名称下调用func。在没有名称的全局名称空间中定义了一些诸如Open之类的功能,因此:: Open调用来自Global Namespace的函数open。它用于避免调用i2cdevice :: Open,并明确调用操作系统的open函数。

name::func() means to calling func under the namespace name. Some functions like open are defined in the global namespace which has no name, so ::open calls a function named open from the global namespace. It is used here to avoid calling I2CDevice::open and to explicitly call the operating system's open function.

染墨丶若流云 2025-02-16 14:48:26

进行函数调用时,您可以将函数的命名空间前缀为:这

<NameSpaceDecl>::<Function>(<Param>);

通常是这样的:

std::sort(begin, end);

但是全局名称空间中的事物可以通过较短的::访问,

::open(<Stuff>);

因此这是一种方式当编译器可能从本地范围中选择一个函数时,指定特定函数。

在您的情况下,您可能需要使用:: open(),因为您的类具有函数i2cdevice :: open(),并且编译器将尝试使用一个(和生成错误)。但是,通过使用:: Open()特定使用,您说的是使用全局名称空间中的open()的版本。

When doing a function call you can prefix the function with the namespace it is in:

<NameSpaceDecl>::<Function>(<Param>);

This usually looks like this:

std::sort(begin, end);

But things in the global namespace, can be accessed by the shorter ::

::open(<Stuff>);

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 function I2CDevice::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 of open() that is in the global namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文