c++这段代码没有域操作符为什么可以运行
阿神
阿神 2017-04-17 15:00:49
0
2
458

问题标出了, A(); //不应该A::A()吗?

#include <iostream>
#include <string>
using namespace std;

class A
{
public:
    A()
    {
        cout << "A constructor" << endl;
    }

    ~A()
    {
        cout << "A destructor" << endl;
    }
};

int main()
{
    A();  //不应该A::A()吗?

    cout<<"end xxx\n";
    cout<<"end yyy\n";

}
阿神
阿神

闭关修行中......

reply all(2)
阿神

The normal call should be

int main()
{
    A a = A();  //不应该A::A()吗?

    cout<<"end xxx\n";
    cout<<"end yyy\n";

}

An analogy to a function that returns a value

int add(int a, int b);

You can call it like this

int c = add(1, 2);

Of course you can also call it like this

add(1, 2);
阿神

Because this is a constructor. . . So this is equivalent to creating a temporary object. Why does the constructor have to write the class name twice

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template