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

查看:86

问题描述

我正在学习 C++,但我不知道何时需要使用 :: .我知道我需要在 coutcin 前面使用 std::.这是否意味着在 iostream 文件中,创建它的开发人员创建了一个名为 std 的命名空间并将函数 cincout 进入名为 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 :: .

例如,如果我创建一个名为 Aclass ,为什么我需要将 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?

推荐答案

您对 coutcin 的看法非常正确.它们是定义在 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:

标题<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;
}

:: 被称为作用域解析操作符.coutcin 的名称是在 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 的类的成员.它们具有相同的名称,因为它的构造函数.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.

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