我想知道关于(::)在c ++中的一切 [英] I want to know everything about (::) in c++

查看:84
本文介绍了我想知道关于(::)在c ++中的一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我见过很多这种语句(:)。我知道它用于在类中声明一个类成员函数(左边的点必须是一个类,右边的点必须是一个函数,是吗?)。但我见过很多不同类型的东西。我不知道这些是什么意思



ei:



Hi
I have seen many type of this statement ( :: ). I know it use for declaring a class member function out of the class( The left point must be a Class , and the right point must be a function , true? ) . But I have seen many different types of that . I don''t know what are those means

e.i :

std::vector<cv::Vec3f>::iterator itrCircles;

OR

QString::number((*itrCircles)[2], 'f' , 3).rightJustified(7,' '));





最后我想知道,当我们没有为func和public,private等定义函数或类类型(void,int等)时对于类)什么类型将指定为默认值?





提前谢谢!



At the end I want know , when we don''t define the function or class type ( void , int , etc for func and public , private , etc for class) what type will assign as default ?


Thank you in advance !

推荐答案

到目前为止给出的两个答案都没有完全解释问题中给出的例子。因此,让我在这里添加第三个答案。



::在C ++中用作范围分隔符。用简单的英语表示,它将类,结构和名称空间的名称分开。正如您在问题中提到的,您已经理解了从成员名称中分离类名和结构名的作用,例如:

Both answers given so far have not fully explained the examples given in the question. Therefore, let me add a third answer here.

The :: is used in C++ as scope separator. In plain English that means, it separates the names of classes, structs, and namespaces. As you have mentioned in your question, you have already understood the role in separating class names and struct names from member names, for example:
int MyClass::MyFunction()
{
    ...
}



另一个常见的用法是命名空间。许多库在命名空间内声明其所有类和函数,以避免与其他库和用户代码冲突。一个众所周知的例子是STL标准库,它将所有名称都包含在名为std的名称空间中。所以,要使用例如STL的 swap 函数,你会写:


The other common usage is with namespaces. Many libraries declare all their classes and functions inside a namespace to avoid conflicts with other libraries and user code. A well known example is the STL standard library, which encloses all its names in a namespace called "std". So, to use for example STL''s swap function you would write:

std::swap (a, b);



或使用你要编写的STL字符串类


or to use the STL string class you would write

std::string myString;



如果没有名称冲突的风险,你可以让生活变得更轻松,并告诉编译器在搜索时也会查看命名空间std一个名字说:


If there is no risk of name conflicts you can make your life a little easier and tell the compiler to also look into namespace "std" whenever it searches for a name by saying:

using namespace std;


源文件开头的
。然后你可以在abour例子中省略std ::前缀。



注意名称空间可以嵌套,所以不要惊讶地看到:


at the beginning of a source file. Then you can omit the std:: prefix in the abour examples.

Note that namespaces can be nested, so don''t be surprised to see:

BigLib::SubSection::SuperSmartClass myClass;



您的上一个问题与该主题无关:在没有类型定义的情况下,编译器分配的默认类型是什么?



答案:在过去用来输入 int 的C语言中;但是新代码不应该使用它。在C ++中,不再是默认类型。如果您忘记指定类型,编译器将发出错误消息 - 这是有充分理由的。通过简单地忘记函数的返回类型并假设它是 int 或者通过模糊地使用int和bool来发生许多编程错误。


Your last question was unrelated to that subject: What is the default type the compiler assigns in the case of absence of a type definition?

Answer: In the C language that used to type "int" in the old days; but new code should not make use of that. In C++ there is no longer a default type. The compiler will issue an error message if you forget to specify the type -- for a good reason. Many programming errors occurred by simply forgetting about the return type of a function and assuming it was int or by the ambiguous use of int and bool.


范围解析运算符::(仅限C ++)

::(范围解析)运算符用于限定隐藏名称,以便您仍然可以使用它们。如果命名空间范围或全局范围名称被块或类中的同名显式声明隐藏,则可以使用一元范围运算符。例如:



Scope resolution operator :: (C++ only)
The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example:

int count = 0;

int main(void) {
  int count = 0;
  ::count = 1;  // set global count to 1
  count = 2;    // set local count to 2
  return 0;
}



main函数中声明的计数声明隐藏了在全局命名空间范围内声明的名为count的整数。语句:: count = 1访问在全局命名空间范围内声明的名为count的变量。



您还可以使用类范围运算符来限定类名或类成员名。如果隐藏了类成员名称,则可以通过使用其类名和类作用域运算符对其进行限定来使用它。



在以下示例中,声明变量X隐藏类类型X,但您仍然可以通过使用类类型X和范围解析运算符对其进行限定来使用静态类成员计数。


The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.

You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.

In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.

#include <iostream>
using namespace std;

class X
{
public:
      static int count;
};
int X::count = 10; // define static data member

int main ()
{
      int X = 0; // hides class type X
      cout << X::count << endl; // use static member of class X
}
</iostream>





资料来源: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm .xlcpp8l.doc%2Flanguage%2Fref%2Fcplr175.htm [ ^ ]


范围解析的主要用户 c ++ 中的operator(::)如下所示..



1。访问全局变量

Main user of Scope resolution operator ( :: ) in c++ is as below..

1. To access global variable
int a=50;

int main()
{
    int a=10; // local variable 'a'
    ::a=100; // accessing global varable 'a'
    cout << ::a << endl; // print global variable
}





2。访问类成员



2. To access members of class

Class MyClass
{
  int n1, n2;
  public:
  {
     void func1(); //Function Declaration
  }
};

public void MyClass::func1() //Use of Scope Resolution Operator to write function definition outside class definition
{
               // Function Code
}


这篇关于我想知道关于(::)在c ++中的一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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