关于构造函数 c++

来源:互联网 发布:重生之星际淘宝主微盘 编辑:程序博客网 时间:2024/05/28 16:01
class F { public:      F() {}      F(int) {}      void f() {}};void test() {      F f1;      F f2();      f1.f();   //可以正常执行      f2.f();   //会报错,因为编译认为F f2();是定义了一个返回值为F类型的函数,f2并不是F的对象,也无f()方法。}



关于 new F() 和 new F 的区别。


class F { public:      int a;      int b;};void test() {      F *f1 = new F;      F *f2 = new F();      cout << f1->a << endl; //此时输出值为不确定的,根据内存状态而定      cout << f2->a << endl; //此时是输出0;      // 这是因为当F时PoD类型时,其成员变量会初始化;      // POD类型是指一个类或对象,其成员是原生数值类型(int, unsigned char, float, bool等等)      // 或者另外一个POD类型。POD对象看起来像C语言风格的结构对象}


0 0
原创粉丝点击