C++ 默认构造函数,用新对象初始化指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14646696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:33:24  来源:igfitidea点击:

C++ default constructor, initializing pointer with new object

c++classpointersdynamicdefault-constructor

提问by Alex

I have the following problem: In myClass I want to default initialize a pointer to yourClass, with a new yourClass adress. Unfortunately, if I want to delete the pointer at any point I get a (core dump).

我有以下问题:在 myClass 中,我想默认初始化一个指向 yourClass 的指针,并使用新的 yourClass 地址。不幸的是,如果我想在任何时候删除指针,我会得到一个(核心转储)。

class myClass
{
      protected:
      yourClass * yc;

      public:
      myClass() { yc = new yourClass(); }

      myClass(yourClass * tyc ) { delete yc; yc = tyc; }

      ~myClass() { delete yc; yc = NULL; }

      void setMyClass (yourClass * tyc) { delete yc; yc = tyc; }

      void print () { yc->print(); }
};

int main()
{
  yourClass b (//parameter);
  myClass * a = new myClass();
  a->print();
  a->setMyClass(&b)
  a->print();

  delete a;
  return 0;
}

The print() of a, should result in two different prints, dependent on //parameters.

a, 的 print() 应该导致两个不同的打印,这取决于 // 参数。

I considered yourClass yc; instead of a yourClass* yc, but I want to know if it is possible.

我考虑过你的Class yc;而不是 yourClass* yc,但我想知道是否可能。

EDIT:I reworked the code in the following way and it works. Still looks complicated, smart pointers seem promising and I still did not apply the "Rule of Three". Here the code. Thanks all.

编辑:我按以下方式重新编写了代码并且它有效。看起来仍然很复杂,智能指针似乎很有希望,我仍然没有应用“三分法则”。这里是代码。谢谢大家。

class myClass
{
      protected:
      yourClass * yc;
      bool dynamic;

      public:
        myClass() { dynamic = true; yc = new yourClass (); }
        myClass (yourClass * tyc ) 
        { 
          // dynamic init (like default)
          if (tyc == NULL ) { dynamic = true; yc = new yourClass (); }
          // static use of yc
          else { dynamic = false; yc = tyc; } 
        }
        // because only if dynamic is true, we need to erase
        ~blu () { if (dynamic) { delete yc; dynamic = false; } } 

        void setMyClass(yourClass* tyc) 
        { 
          // leaving unchanged if new-stuff is NULL or like old-stuff
          if ( tyc == yc || tyc == NULL ) return;
          else // treating dynamic and static differently
          { 
            if (dynamic) // if flag is set, must be deleted 
            {
              delete yc; yc = tyc; dynamic = false;
            }
            else // must not be deleted, dynamic is still false
            {
              yc = tyc;
            }
          }
        }
        void print () { yc->print(); }
};

回答by Lorenzo Dematté

That's because you are trying to delete too much:

那是因为你试图删除太多:

  • you are deleting a non-allocated object in the second constructor (remove delete yc;)
  • you are trying to delete a stack-allocated object, b. delete a;will try to delete a pointer to b, which is an object on the stack; what happens depend on your OS (I expect an exception/core dump/whatever)
  • 您正在删除第二个构造函数中的未分配对象(remove delete yc;
  • 您正在尝试删除堆栈分配的对象 b。delete a;将尝试删除指向 b 的指针,它是堆栈上的一个对象;会发生什么取决于你的操作系统(我希望有一个异常/核心转储/任何)

EDIT: another problem I spotted.. a->setMyClass(NULL)

编辑:我发现的另一个问题.. a->setMyClass(NULL)

I would suggest:

我会建议:

  • this poston smart pointers
  • this blog poston RAII
  • any C/C++ primer explaining stack vs. heap allocation (static vs. dynamic?)
  • 这篇关于智能指针的帖子
  • 这篇关于 RAII 的博文
  • 任何解释堆栈与堆分配的 C/C++ 入门(静态与动态?)

回答by sehe

You're violating the rule of three.

你违反了三原则。

What is The Rule of Three?

三分法则是什么?

Also this is recipe for disaster:

这也是灾难的秘诀:

 myClass(yourClass * tyc ) { delete yc; yc = tyc; }

What happens if tyc==yc? Right. Not pretty :)

如果会发生什么tyc==yc?对。不漂亮 :)

 myClass(yourClass * tyc ) { if (yc!=tyc) { delete yc; yc = tyc; } }