0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ > スライス問題 > 派生クラスを渡したときにこの派生クラスに他のフィールドがあると、それが切り取られる > const参照で引数を渡す

Last updated at Posted at 2015-08-03

引用: C++のためのAPIデザイン by マーティン・レディ
7.1

void SetObjectByValue(MyObject obj);

あるメソッドが基本クラスの引数を(値で)受け取った場合、あなたが派生クラスを渡したときにこの派生クラスに他のフィールドがあると、それが切り取られるという問題だ。これは、値で渡されるオブジェクトのサイズは、コンパイル時に、基本クラスのサイズであると判断されるからだ。値ではなく、const参照で引数を渡せば、このスライス問題を回避できる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?