何时使用“::”以及何时使用“。”

时间:2012-07-11 22:12:35

标签: c++

对于我认为非常基本的问题抱歉。

我无法在网上找到运营商::和之间的差异。在C ++中

我有几年的C#和Java经验,熟悉使用的概念。成员访问的运营商。

有人可以解释何时会使用这些以及它们有什么区别?

感谢您的时间

7 个答案:

答案 0 :(得分:23)

区别在于第一个是范围解析运算符,第二个是成员访问语法。

因此,::(范围解析)可用于访问命名空间中的某些内容,如嵌套类,或访问静态函数。 .句点运算符将只访问您正在使用它的类实例的任何可见成员。

一些例子:

class A {
    public:

        class B { };

        static void foo() {}
        void bar() {}
};

//Create instance of nested class B.
A::B myB; 

//Call normal function on instance of A.
A a;
a.bar();

//Call the static function on the class (rather than on an instance of the class). 
A::foo(); 

请注意,静态函数或数据成员是属于类本身的成员,无论您是否已创建该类的任何实例。所以,如果我在我的类中有一个静态变量,并且包含了该类的一千个实例,那么该静态变量只有一个实例。但是,任何其他成员的1000个实例都不是静态的,每个类的实例一个。

当你来到它时,还有一个有趣的选择:)你也会看到:

//Create a pointer to a dynamically allocated A.
A* a = new A();

//Invoke/call bar through the pointer.
a->bar();

//Free the memory!!! 
delete a;

如果你尚未学习动态内存可能会有点混乱,所以我不会详细介绍。只是希望您知道可以使用{::.->}访问成员:)

答案 1 :(得分:6)

C ++中的

::是范围解析运算符。它用于区分名称空间和静态方法,基本上任何情况下你都没有对象。其中.用于访问对象内的内容。

C#对它们都使用.运算符。

namespace Foo
{
    public class Bar
    {          
        public void Method()
        {
        }

        public static void Instance()
        {
        }

    }
}
在C#中你会写这样的代码:

var blah = new Foo.Bar();
blah.Method();

但是等效的C ++代码看起来更像是这样:

Foo::Bar blah;
blah.Method();

但请注意,也可以使用范围解析运算符访问静态方法,因为您没有引用对象。

Foo::Bar::Instance();

同样,C#代码只使用点运算符

Foo.Bar.Instance();

答案 2 :(得分:4)

::用于命名空间和静态成员访问。 C#使用点运算符代替名称空间。

.用于非静态成员访问。

不是详尽的描述,但根据C#和Java,可能会让您感到困惑。

有关详细信息,请参阅

IBM - Scope Resolution Operator

IBM - Dot Operator

答案 3 :(得分:2)

::是范围解析运算符,因此当您解析范围(例如命名空间或类)时,可以使用它。要获得会员访问权限,您需要.

答案 4 :(得分:1)

如果您不了解名称空间或类,则可能很难理解作用域运算符::。命名空间就像是代码中各种内容名称的容器。它们通常用于消除库中通用名称的歧义。假设两个名称空间stdexample都具有函数foobar()。因此,编译器知道您要使用哪个函数,将其作为std::foobar()example::foobar()添加。

当告诉编译器要定义在类或结构中声明的函数时,也可以使用::运算符。例如:

class foobar()
{
  public:
  void hello();
  int number; //assume there is a constructor that sets this to 5
}

void foobar::hello()
{
  cout << "Hello, world!" << endl;
}

当您希望使用类或结构的成员时,将使用.运算符。例如:

foobar foo;
foo.hello();
cout << foo.number << endl;

假设通过编写构造函数完成了类,那么它的输出应该是:

Hello, world!
5

答案 5 :(得分:0)

当在程序中创建类的成员时访问类的成员时,在java中使用.运算符。 ::用于多个案例:

在某个类的.h / .cpp中定义方法时,请写

class::methodName()

用于原型设计或实施。

此外,如果您没有明确说明您使用的命名空间,则必须使用它

std::cout << "This is the output";

而不是仅仅使用cout << "This is the output;

也许还有更多,但我现在不记得了,我的C ++有点生疏。

答案 6 :(得分:0)

在C ++中,::用于标识范围。这可能意味着命名空间范围 类范围

例如

int x;

namespace N {
    int x;

    struct foo {
        static double x;
        double y;    
    };
    struct bar: public foo {
        double y;
    };
}

int main()
{
    int x; // we have a local, hiding the global name
    x = ::x; // explicitly identify the x in global scope
    x += N::x; // explicitly identify the x in namespace N

    N::foo::x = x; // set the static member of foo to our local integer
    N::foo f;
    f.y = f.x; // the static member is implicitly scoped by the object
    f.y += N::foo::x; // or explicitly scoped

    N::bar b;
    assert(b.x == N::foo::x); // this static member is inherited
    b.y = b.x; // we get N::bar::y by default
    b.N::foo::y = b.y; // explicitly request the hidden inherited one
}

// we need to define the storage for that static somewhere too ...
int N::foo::x (0.0);
相关问题