到底这两句有什么区别?

发布于 2022-10-15 07:19:05 字数 912 浏览 28 评论 0

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6.     virtual void foo()
  7.         {
  8.                 cout<<"in A"<<endl;
  9.         }
  10. };
  11. class B: public A
  12. {
  13.     virtual void foo()
  14.         {
  15.                 cout<<"in B"<<endl;
  16.         }
  17. };
  18. main()
  19. {
  20.     A *p = new B();  //(1)
  21.     //A *p = new B;  //(2)
  22.     p->foo();
  23. }

复制代码请问:上述程序中(1)与 (2)两句到底有什么区别?谢谢!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

独木成林 2022-10-22 07:19:05

没有高人会?{:3_182:}{:3_182:}{:3_182:}

弄潮 2022-10-22 07:19:05

没区别

<<C++ Primer>> 12.4. Constructors

Using the Default Constructor

The correct way to define an object using the default constructor is to leave off the trailing, empty parentheses:
// ok: defines a class object ...
     Sales_item myobj;
On the other hand, this code is fine:
// ok: create an unnamed, empty Sales_itemand use to initialize myobj
     Sales_item myobj = Sales_item();

梦魇绽荼蘼 2022-10-22 07:19:05

#include <iostream>
using namespace std;

struct foo
{
        int val;
};

int main()
{
        foo* p1 = new foo;
        foo* p2 = new foo();
   
        cout << p1->val << endl; // output: 0
        cout << p2->val << endl; // output: random value

    return 0;
}

枉心 2022-10-22 07:19:05

回复 4# bruceteen

反了,带 () 的会初始化为 0。

趴在窗边数星星i 2022-10-22 07:19:05

#include
using namespace std;

struct foo
{
        int val;
};

int main()
{
        foo* p1 = new foo ...
bruceteen 发表于 2011-05-11 13:24

    两个都是0

桃气十足 2022-10-22 07:19:05

本帖最后由 zhuqing_739 于 2011-05-11 15:52 编辑

我把下面的程序运行了一下:

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6.         A()
  7.         {
  8.                 cout<<"A construct function"<<endl;
  9.         }
  10.         virtual ~A()
  11.         {
  12.                 cout<<"A deconstruct function"<<endl;
  13.         }
  14.     virtual void foo()
  15.         {
  16.                 cout<<"in A"<<endl;
  17.         }
  18.         void hello()
  19.         {
  20.                 cout<<"hello! the world!"<<endl;
  21.         }
  22. };
  23. class B: public A
  24. {
  25. public:
  26.         B()
  27.         {
  28.                 cout<<"B construct function"<<endl;
  29.         }
  30.         ~B()
  31.         {
  32.                 cout<<"B deconstruct function"<<endl;
  33.         }
  34.     virtual void foo()
  35.         {
  36.                 cout<<"in B"<<endl;
  37.                 cout<<x<<endl;
  38.         }
  39. private:
  40.         int x;
  41. };
  42. main()
  43. {
  44.     A *p = new B;  //(1)
  45.         //A *p = new B;  //(2)
  46.     p->foo();
  47.         delete p;
  48. }

复制代码带括号的结果:

2.jpg (16.6 KB, 下载次数: 2)

下载附件

2011-05-11 15:49 上传



不带括号的结果:

1.jpg (17.75 KB, 下载次数: 2)

下载附件

2011-05-11 15:52 上传



两个结果一样的!!!!!!
怎么解释?

半暖夏伤 2022-10-22 07:19:05

一樣。

随梦而飞# 2022-10-22 07:19:05

对于内置类型前一个会初始化为0,后者不会;自定义类型,就没区别了,都是默认构造函数

耳根太软 2022-10-22 07:19:05

9楼说的对

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文