在C ++中使用:: [英] Using :: in C++

查看:76
本文介绍了在C ++中使用::的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,我永远无法确定何时需要使用 :: 。我确实知道我需要在 cout cin <前使用 std :: / code>。这是否意味着在 iostream 文件中,创建该文件的开发人员创建了一个名为 std 的命名空间,并将函数 cin cout 进入名为 std 的命名空间?当出于某种原因创建与 main()不在同一个文件中的新类时,我必须添加 ::

I am learning C++ and I can never tell when I need to use :: . I do know that I need to use std:: in front of cout and cin. Does this mean that inside of the iostream file the developers that created it made a namespace called std and put the functions cin and cout into the namespace called std? When I created a new class that isn't in the same file as main() for some reason I must add :: .

例如,如果我创建一个名为 A c $ c>,为什么我没有将 A :: 放在我创建的函数前面,即使我没有将它放在名字上也是如此?例如 void A :: printStuff(){} 。如果我在 main 中创建一个函数,为什么不必须放置 main :: printStuf {}

For example, if I create a class called A , why do I need to put A:: in front of a function that I make, even though I didn't put it into a namesace? For example void A::printStuff(){} . If I create a function in main, why don't I have to put main::printStuf{}?

我知道我的问题可能令人困惑,但是有人可以帮助我吗?

I know that my question is probably confusing, but could someone help me?

推荐答案

您对 cout cin 非常正确。它们是在 std 命名空间内定义的对象(不是函数)。以下是根据C ++标准定义的声明:

You're pretty much right about cout and cin. They are objects (not functions) defined inside the std namespace. Here are their declarations as defined by the C++ standard:


Header < iostream> 摘要

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

namespace std {
  extern istream cin;
  extern ostream cout;
  extern ostream cerr;
  extern ostream clog;

  extern wistream wcin;
  extern wostream wcout;
  extern wostream wcerr;
  extern wostream wclog;
}


:: 被称为作用域分辨率运算符。名称 cout cin std 中定义,因此我们必须使用 std :: 来限定其名称。

:: is known as the scope resolution operator. The names cout and cin are defined within std, so we have to qualify their names with std::.

类的行为有点类似于名称空间,因为在类内部声明的名称属于上课。例如:

Classes behave a little like namespaces in that the names declared inside the class belong to the class. For example:

class foo
{
  public:
    foo();
    void bar();
};

名为 foo 的构造函数是成员 foo 的类的em>。它们具有相同的名称,因为其构造函数。函数 bar 也是 foo 的成员。

The constructor named foo is a member of the class named foo. They have the same name because its the constructor. The function bar is also a member of foo.

因为它们是 foo 的成员,当从班级外部引用它们时,我们必须限定其名称。毕竟,它们属于该类。因此,如果要在类外定义构造函数和 bar ,则需要这样做:

Because they are members of foo, when referring to them from outside the class, we have to qualify their names. After all, they belong to that class. So if you're going to define the constructor and bar outside the class, you need to do it like so:

foo::foo()
{
  // Implement the constructor
}

void foo::bar()
{
  // Implement bar
}

这是因为正在定义它们在课程之外。如果您没有在名称上加上 foo :: 限定符,那么您将在全局范围内定义一些新功能,而不是作为的成员。 foo 。例如,这是完全不同的 bar

This is because they are being defined outside the class. If you had not put the foo:: qualification on the names, you would be defining some new functions in the global scope, rather than as members of foo. For example, this is entirely different bar:

void bar()
{
  // Implement different bar
}

允许与该函数具有相同的名称在 foo 类中,因为它在不同的范围内。 bar 在全局范围内,而其他 bar 属于 foo 类。

It's allowed to have the same name as the function in the foo class because it's in a different scope. This bar is in the global scope, whereas the other bar belonged to the foo class.

这篇关于在C ++中使用::的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆