小编Gon*_*ado的帖子

Move Semantics C++ 11(Bjarne Stroustrup book,pg75)

我想让我清楚一下移动语义.我正在关注Bjarne Stroustrup第4版的例子,我真的输了.

他说当很多元素(在类向量中)所以移动语义是解决方案时,对象的副本可能很昂贵.

想想像:

矢量结果= vector1 + vector2 + vector3;

顺序可能是错误的,但它会(vector2 + vector3)生成部分结果result1,而result1 + vector1生成结果;

我重载了operator +:

Vector operator+(const Vector &a,const Vector &b)
{

  if(a.size()!=b.size()) {
      throw length_error{"Length of vectors must match for summing"};
  }

  Vector result(a.size());
  cout << "Intermediate result for sum created" << endl;
  for(long unsigned  int i=0; i<result.size();i++){
      result[i] = a[i] + b[i];
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

我还创建了一个移动构造函数:

Vector::Vector(Vector&& orig) noexcept
  :elem{orig.elem},
   sz{orig.sz}
{
  cout << "Move constructor called" << endl;
  orig.elem = nullptr;          // We own …
Run Code Online (Sandbox Code Playgroud)

c++ vector c++11

7
推荐指数
1
解决办法
566
查看次数

GObject与继承

我在做的油嘴一些严重的软件。我意识到有些主题我不太了解。IRC也无济于事...

当我们进行继承时,我们可以有两个类。第一个A直接从GObject继承,B直接从A继承。然后我来到这里:

https://developer.gnome.org/gobject/stable/chapter-gobject.html

static void
viewer_file_constructed (GObject *obj)
{
  /* update the object state depending on constructor properties */

  /* Always chain up to the parent constructed function to complete object
   * initialisation. */
  G_OBJECT_CLASS (viewer_file_parent_class)->constructed (obj);
}

static void
viewer_file_class_init (ViewerFileClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = viewer_file_constructed;
}
Run Code Online (Sandbox Code Playgroud)

但是,当你有这样的安排。子类执行此操作:object_class-> constructed = viewer_file_constructed; 实际上,在B覆盖中唯一构造的内存地址。因此,这意味着G_OBJECT_CLASS(viewer_file_parent_class)-> constructed(obj); 将调用B->递归构成。这不是我们想要的。

也许我听不懂,但我想B中的内存结构是这样的:

struct _B
{
  A parent_instance;

  /* instance members */
};
Run Code Online (Sandbox Code Playgroud)

内部表示应类似于:

[   Gobject struct memory ]
[                         ]
[ …
Run Code Online (Sandbox Code Playgroud)

inheritance gobject glib

0
推荐指数
1
解决办法
687
查看次数

标签 统计

c++ ×1

c++11 ×1

glib ×1

gobject ×1

inheritance ×1

vector ×1