【问题标题】:How are the Constructor and Destructors called构造函数和析构函数如何调用
【发布时间】:2021-07-28 19:13:30
【问题描述】:
#include<iostream>


class Example{
    int i=0,j=0;
    public:
        Example(){
            std::cout<<"Default Constructor is called "<<j++<<std::endl;
        }
        ~Example(){
            std::cout<<"Destructor is Called "<<i++<<std::endl;
        }
        void display(){
            std::cout<<"Display method called "<<std::endl;
        }
};

void function(){
    Example e;
    e.display();
}

int main(){

 function();
Example e1;

}

我正在尝试查看如何为同一类的两个对象调用构造函数和析构函数

我得到的输出是

Default Constructor is called 0
Display method is called
Destructor is called 0
Default Constructor is called 0
Destructor is called 0

为什么我的 i 和 j 变量没有递增

【问题讨论】:

  • 您的变量会增加,但您不会打印新值。
  • 试试++j++i。也许(取决于你的意图)声明它们static
  • 一旦从 post 更改为 pre increment,请注意每个对象都有自己的成员变量副本,并且两个对象的成员变量将被初始化为相同的值:零。

标签: c++ constructor destructor


【解决方案1】:

您的 ij 变量会递增,但不会打印结果。

你使用了后缀自增运算符,所以它被评估为before自增的值。

正如你所说,有两个对象,它们具有独立的成员变量ij,因为它们不是静态的。

【讨论】:

    【解决方案2】:

    由于两个对象彼此独立,因此递增的值不会影响另一个对象。它们是递增的,如果您想查看递增的值,则可以尝试使用 ++i 或 ++j 而不是 i++ 或 j++ 进行预递增。 想了解更多请发表评论。

    【讨论】:

      【解决方案3】:

      您有 2 个不同的 示例 类对象,即 ee1。其中e属于你定义的function函数,e1属于main函数。

      因此,当您执行程序时,对象 e 只会被调用一次,并且该对象的 ij 的值会递增。但随后您使用另一个对象e1,其值不同于以前称为对象e。这就是为什么 ij 的值对于不同的对象是不同的。每次您创建一个新对象时,它们都会为该对象初始化为 0。

      现在,要使所有对象的这些值相同,您必须做的是使这些变量static。静态将使您创建的同一类的所有对象都可以访问变量,但值不变,并且每次创建新对象时它们都不会被初始化。

      你可以这样做:

      static int i,j;
      

      但请确保不要在类中初始化静态变量。否则程序会抛出错误。因此,要将静态变量初始化为 0,请在类之外访问它们并将它们设置为您希望初始化它们的任何值。如下:

      int Example::i = 0;
      int Example::j = 0;
      

      就是这样。现在您可以实现您想要的。

      所以现在您的代码必须如下所示:

      class Example{
          static int i,j;
          public:
              Example(){
                  std::cout<<"Default Constructor is called "<<j++<<std::endl;
              }
              ~Example(){
                  std::cout<<"Destructor is Called "<<i++<<std::endl;
              }
              void display(){
                  std::cout<<"Display method called "<<std::endl;
              }
      };
      
      int Example::i = 0;
      int Example::j = 0;
      
      void function(){
          Example e;
          e.display();
      }
      
      int main(){
       function();
       Example e1;
      }
      

      【讨论】:

        【解决方案4】:

        已进行更改。请再运行一次

        #include<iostream>
        
        
        class Example{
            static int i,j; //Changed
            public:
                Example(){
                    std::cout<<"Default Constructor is called "<<j++<<std::endl;
                }
                ~Example(){
                    std::cout<<"Destructor is Called "<<i++<<std::endl;
                }
                void display(){
                    std::cout<<"Display method called "<<std::endl;
                }
        };
        int Example::i = 0; //initializing i and j like this outside the class
        int Example::j = 0; //Added
        
        void function(){
            Example e;
            e.display();
        }
        
        int main(){
        
         function();
        Example e1;
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-16
          • 1970-01-01
          • 1970-01-01
          • 2013-07-07
          • 2013-06-24
          • 1970-01-01
          • 2011-04-03
          相关资源
          最近更新 更多