views:

109

answers:

4

In C++, if an object of a class is passed as a parameter into a function, the copy constructor of the class will be called.

I was wondering if the object is of nonclass type, what function will be called?

Similarly in C, what function is called when passing values or address of variables into a function?

Thanks and regards!

+6  A: 

No function will be called; the bytes composing the object will simply be copied to the correct place for the callee (be that a location in memory or a register).

James McNellis
+2  A: 

No function is called.

Since non-object types don't have methods, they are simply copied onto the stack to be used as-is by your function.

Chris Cooper
+3  A: 

The copy constructor is only called if the object is being passed by value (and is a non-POD type). This is one of the reasons that it is common practice to pass objects by reference and const reference should you not wish the object to be changed by the function.

Johnsyweb
+1  A: 

It depends on the implementation, but in some cases you may incur a function call if you are passing a floating-point value into a function expecting a value of integral type. (This is an implementation detail rather than part of the language, it's true, but it's no less worth taking account of because of that. And such conversions are often slow in any event, function call required or not.)

brone