0

I have this function:

int foo(bar const&);

I want to use it like this:

bar a;
foo(a);

Here no copy of a will be happened.

But if I call it like this:

bar* a = new bar(); //not about dynamic allocation, I know it is not good
foo(*a);

At foo(*a);, will I loose some performance? Like unnecessary copy?

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160

2 Answers2

2

No copy will happen in either case. You can easily confirm that for instance by deleting the copy constructor of bar:

bar(bar const&) = delete; // cannot be copied
bar& operator=(bar const&) = delete; // cannot be reassigned

This would lead to a compiler error in case any bar object is being copied.

Zulan
  • 21,896
  • 6
  • 49
  • 109
1

No, there won't be a copy. Given a value a of type T*, the expression *a has type T& - i.e. you're passing a reference.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207