RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 609340
Accepted
yrHeTateJlb
yrHeTateJlb
Asked:2020-12-30 02:56:38 +0000 UTC2020-12-30 02:56:38 +0000 UTC 2020-12-30 02:56:38 +0000 UTC

使用新的(这个)

  • 772

阅读C++ 超级常见问题解答。在构造函数部分,我看到了以下语句:

顺便说一句,不要试图通过放置新的来实现这一点。有些人认为他们可以在 Foo::Foo(char) 的主体内说 new(this) Foo(x, int(x)+7) 。然而,那是不好的,不好的,不好的。请不要写信告诉我它似乎适用于您的特定编译器的特定版本;这不好

关键是绝对不可能这样做:

class Foo{
public:
    Foo(char x){
        new (this) Foo(x, int(x)+7); 
    }
    Foo(char x, int y){
        //...
    }
};

任何人都可以更详细地解释这个技巧威胁到什么吗?

UPD:我怀疑在这个例子中一切都会好起来的,问题将从继承、动态资源分配等开始。

c++
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Harry
    2020-12-30T04:46:46Z2020-12-30T04:46:46Z

    http://ideone.com/gkgi7S :

    class Base
    {
    public:
        Base()  { ptr = new int[100]; cout << "alloc mem at " << ptr << endl; }
        ~Base() { delete [] ptr; cout << "free mem at " << ptr << endl; }
        int * ptr;
    };
    
    class Derived: public Base
    {
    public:
        Derived(int x, int y):x(x),y(y){}
        Derived(int x)
        {
            new(this) Derived(x,0);
        }
        int x, y;
    };
    
    int main(int argc, const char * argv[])
    {
        Derived d(5);
    }
    

    结论:

    alloc mem at 0070EA58
    alloc mem at 0070FFE8
    free mem at 0070FFE8
    

    这个例子够吗?

    没有继承是可能的 - 本质不会改变:

    class Derived
    {
    public:
        Derived(int x, int y):x(x),y(y){}
        Derived(int x)
        {
            new(this) Derived(x,0);
        }
        int x, y;
        Base b;
    };
    
    • 4
  2. isnullxbh
    2020-12-30T04:36:28Z2020-12-30T04:36:28Z

    上帝在地球上的总督Stroustrup 写道:链接

    使用示例:

    /*1*/ char *pBuf  = new char[sizeof(string)]; 
    /*2*/ string *p = new (pBuf) string("hi");    
    /*3*/ string *q = new string("hi");
    

    在第一行中,我们分配内存,在第二行中 - 我们在已分配的内存区域上构建一个对象。这个例子是平庸的,但你会抓住本质:我们正在一个预先分配的内存区域上构建一个对象,我们可以确定不会拒绝分配内存 - 毕竟它已经在所需的位置分配了“音量”,可以这么说。一个真实的例子是一些性能很重要的关键部分,没有异常等。

    放置运算符,例如:

    const SomeoneClass& SomeoneClass::operator=( const SomeoneClass& other) {
       if ( this != &other ) {
          this->~SomeoneClass();
          new (this) SomeoneClass(other);
       }
       return *this;
    }
    

    好吧,请注意 Stroustrup 写的关于在这种情况下释放内存的内容。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5