Click here to Skip to main content
15,912,457 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionwhy passing argument by reference(&ref) in a template function shows error ? but works fine if arguments passed by pointer(*ptr) ? Pin
Tarun Jha13-May-18 1:46
Tarun Jha13-May-18 1:46 
in the below program it i have used both ways i.e. passed arguments by reference and pointers.

1. Arguments by reference:

#include <iostream>
using namespace std;

template<class T>
void swap(T &x, T &y){
    T temp = x;
    x = y;
    y = temp;
}

void fun(int m, int n, float a, float b){

    cout << "m & n before swap: " << m << " " << n << endl;
    swap(m, n);
    cout << "m & n after swap: " << m << " " << n << endl;

    cout << "i & j before swap: " << a << " " << b << endl;
    swap(a, b);
    cout << "i & j after swap: " << a << " " << b << endl;
}

int main(){
    fun(100, 200, 11.22, 33.44);

    return 0;
}


2. Arguments by pointers:
#include <iostream>
using namespace std;

template<class T>
void swap(T *x, T *y){
    T temp = x;
    x = y;
    y = temp;
}

void fun(int m, int n, float a, float b){

    cout << "m & n before swap: " << m << " " << n << endl;
    swap(m, n);
    cout << "m & n after swap: " << m << " " << n << endl;

    cout << "i & j before swap: " << a << " " << b << endl;
    swap(a, b);
    cout << "i & j after swap: " << a << " " << b << endl;
}

int main(){
    fun(100, 200, 11.22, 33.44);

    return 0;
}



also if i pass the arguments by reference but make it a constant then also i works, why ?
#include <iostream>
using namespace std;

template<class T>
void swap(const T &x, const T &y){
    T temp = x;
    x = y;
    y = temp;
}

void fun(int m, int n, float a, float b){

    cout << "m & n before swap: " << m << " " << n << endl;
    swap(m, n);
    cout << "m & n after swap: " << m << " " << n << endl;

    cout << "i & j before swap: " << a << " " << b << endl;
    swap(a, b);
    cout << "i & j after swap: " << a << " " << b << endl;
}

int main(){
    fun(100, 200, 11.22, 33.44);

    return 0;
}


Thank you.
AnswerRe: why passing argument by reference(&ref) in a template function shows error ? but works fine if arguments passed by pointer(*ptr) ? Pin
CPallini13-May-18 2:56
mveCPallini13-May-18 2:56 
GeneralRe: why passing argument by reference(&ref) in a template function shows error ? but works fine if arguments passed by pointer(*ptr) ? Pin
Tarun Jha26-May-18 9:50
Tarun Jha26-May-18 9:50 
GeneralRe: why passing argument by reference(&ref) in a template function shows error ? but works fine if arguments passed by pointer(*ptr) ? Pin
CPallini26-May-18 10:53
mveCPallini26-May-18 10:53 
Questionproblem encountered by calling constructors explicitly. Pin
Tarun Jha11-May-18 10:20
Tarun Jha11-May-18 10:20 
AnswerRe: problem encountered by calling constructors explicitly. Pin
Joe Woodbury11-May-18 11:39
professionalJoe Woodbury11-May-18 11:39 
GeneralRe: problem encountered by calling constructors explicitly. Pin
Tarun Jha11-May-18 11:57
Tarun Jha11-May-18 11:57 
AnswerRe: problem encountered by calling constructors explicitly. Pin
CPallini12-May-18 4:13
mveCPallini12-May-18 4:13 
Questionvs2005 Form window Error C2039 Pin
jimNLX11-May-18 6:31
jimNLX11-May-18 6:31 
AnswerRe: vs2005 Form window Error C2039 Pin
Victor Nijegorodov11-May-18 7:23
Victor Nijegorodov11-May-18 7:23 
GeneralRe: vs2005 Form window Error C2039 Pin
jimNLX11-May-18 7:28
jimNLX11-May-18 7:28 
GeneralRe: vs2005 Form window Error C2039 Pin
Victor Nijegorodov11-May-18 8:07
Victor Nijegorodov11-May-18 8:07 
QuestionWhy do I need typecast hex? Pin
Vaclav_11-May-18 5:18
Vaclav_11-May-18 5:18 
AnswerRe: Why do I need typecast hex? Pin
Richard MacCutchan11-May-18 6:25
mveRichard MacCutchan11-May-18 6:25 
GeneralRe: Why do I need typecast hex? Pin
Vaclav_11-May-18 6:34
Vaclav_11-May-18 6:34 
Questionconst char [] and char[] difference question Pin
focusdoit10-May-18 14:26
focusdoit10-May-18 14:26 
QuestionRe: const char [] and char[] difference question Pin
David Crow10-May-18 17:22
David Crow10-May-18 17:22 
AnswerRe: const char [] and char[] difference question Pin
Richard MacCutchan10-May-18 21:25
mveRichard MacCutchan10-May-18 21:25 
AnswerRe: const char [] and char[] difference question Pin
Victor Nijegorodov10-May-18 23:03
Victor Nijegorodov10-May-18 23:03 
GeneralRe: const char [] and char[] difference question Pin
samzcs11-May-18 2:24
samzcs11-May-18 2:24 
QuestionPlease advise me on C++ coding structure Pin
Vaclav_9-May-18 4:24
Vaclav_9-May-18 4:24 
AnswerRe: Please advise me on C++ coding structure Pin
leon de boer9-May-18 4:44
leon de boer9-May-18 4:44 
QuestionHelp with C Pin
Faith Burnett5-May-18 10:28
Faith Burnett5-May-18 10:28 
AnswerRe: Help with C Pin
David Crow5-May-18 16:57
David Crow5-May-18 16:57 
GeneralRe: Help with C Pin
Faith Burnett5-May-18 19:08
Faith Burnett5-May-18 19:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.