Typecast - Template swap

Hi,
i do have a problem of understanding, but may be NOT I.

I do refer to
http://www.cplusplus.com/reference/algorithm/swap/

code snippet:

template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}

This sample in variants can I find via google,
as "the example" for introducing Templates.

As i am a re-beginner,
"i am in the mood"

to see the first instruction : T c(a)
(which is already new for me - and i interprete, better guess, this as :

T C; c=a
- in 2 separated commands declaration of c followed by the assignment of a to c )

+++++

But for my problem i think "c" should be a Pointer type variable

" T * c; "
followed by c=a; and so on...

because the submitted parameters are "adresses, more specifically references"

So:
Am i correct, that the example of the template includes some implicit type casting ?

IF,
in general: I am looking for some documentation especially on implicit type casting regarding the usage of pointers, references and values in any direction.

OR: Am I completely on wrong way ?
----------------------------------

Thank you in advance.




































closed account (DSLq5Di1)
http://en.wikipedia.org/wiki/Copy_constructor
Hi,

OK. Thank you first.

I am still a bit wired.

quote from wiki:
A copy constructor is a special constructor in the C++ programming language creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed ....

So in the Template the passed parameter "a" which is a reference to object a
(lets say: object car)

is used to create an object "c" --of the same type--

NOW i would assume , with T c(a) is a (whole) object of type "car", and not a reference to.

so in further a=c ... i expect typecasting.



I fear, i miss something here .... wired ?!

Thanks







T c(a)


T c(a); is the same as T c = a;

This is similar to T c; c = a;, however it's not exactly the same.

Putting it in one command constructs c as a copy of a. Splitting it up as two commands default constructs c, then assigns a to it.

This will have the same result, but doing it in one command is often more efficient for complex types like strings, or other classes.

But for my problem i think "c" should be a Pointer type variable

" T * c; "
followed by c=a; and so on...


Nope. That would not work.

'a' and 'b' are references, not pointers. There are no pointers in that function.

Besides, the whole point of 'c' here is to be temporary storage for a 'T' so that a and b can be swapped. Holding a pointer would be useless because then you'd have no temporary storage.

Am i correct, that the example of the template includes some implicit type casting ?


Nope. There is no typecasting here. And the fact that it's a template has nothing to do with it. The references seem to be confusing you.

References are simply a way to create an alternate name for a variable. Consider this example:

1
2
3
4
5
6
7
int foo = 0;
int& bar = foo;  // bar is a reference, referring to foo

// this means that bar "is" foo
bar = 5; // this is the same as "foo = 5;", because bar "is" foo

cout << foo; // prints 5 


In your case (and in most cases) references are passed to and from functions. But they have the same purpose:

1
2
3
4
5
6
7
8
9
10
11
12
13
void Triple(int& v)  // v as a reference
{
  v *= 3;  // multiply v by 3
}

int main()
{
  int foo = 2;
  Triple( foo );  // pass 'foo' by reference to 'Triple'
    // changes made to 'v' in Triple will change 'foo'

  cout << foo;  // prints 6, since foo was tripled by Triple
}
Oh yes,

in deed: The references are confusing me. WERE ! Slowly I get into it.

Your comment is a great help.
-------------------------------------

It was the pionter like usage combined with value like syntax, i think.

void foobar(int & bar)
{ bar+=1; } // VALUE like syntax


I think, i got a very useful imagination of references now.


Thank you.
-------------









Last edited on
Topic archived. No new replies allowed.