Click here to Skip to main content
13,799,269 members

C / C++ / MFC

 
GeneralRe: CInternetSession OpenURL Pin
_Flaviu15-May-18 1:21
member_Flaviu15-May-18 1:21 
GeneralRe: CInternetSession OpenURL Pin
Jochen Arndt15-May-18 1:31
mvpJochen Arndt15-May-18 1:31 
GeneralRe: CInternetSession OpenURL Pin
_Flaviu15-May-18 1:44
member_Flaviu15-May-18 1:44 
GeneralRe: CInternetSession OpenURL Pin
Jochen Arndt15-May-18 2:21
mvpJochen Arndt15-May-18 2:21 
AnswerRe: CInternetSession OpenURL Pin
Randor 15-May-18 10:46
professional Randor 15-May-18 10:46 
QuestionWhy can't variables be declared in a switch statement Pin
tracyhe12313-May-18 21:34
membertracyhe12313-May-18 21:34 
AnswerRe: Why can't variables be declared in a switch statement Pin
Richard MacCutchan13-May-18 21:58
protectorRichard MacCutchan13-May-18 21:58 
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 2:46
memberTarun Jha13-May-18 2: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 3:56
mvpCPallini13-May-18 3: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 10:50
memberTarun Jha26-May-18 10: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 11:53
mvpCPallini26-May-18 11:53 
Questionproblem encountered by calling constructors explicitly. Pin
Tarun Jha11-May-18 11:20
memberTarun Jha11-May-18 11:20 
AnswerRe: problem encountered by calling constructors explicitly. Pin
Joe Woodbury11-May-18 12:39
professionalJoe Woodbury11-May-18 12:39 
GeneralRe: problem encountered by calling constructors explicitly. Pin
Tarun Jha11-May-18 12:57
memberTarun Jha11-May-18 12:57 
AnswerRe: problem encountered by calling constructors explicitly. Pin
CPallini12-May-18 5:13
mvpCPallini12-May-18 5:13 
Questionvs2005 Form window Error C2039 Pin
jimNLX11-May-18 7:31
memberjimNLX11-May-18 7:31 
AnswerRe: vs2005 Form window Error C2039 Pin
Victor Nijegorodov11-May-18 8:23
memberVictor Nijegorodov11-May-18 8:23 
GeneralRe: vs2005 Form window Error C2039 Pin
jimNLX11-May-18 8:28
memberjimNLX11-May-18 8:28 
GeneralRe: vs2005 Form window Error C2039 Pin
Victor Nijegorodov11-May-18 9:07
memberVictor Nijegorodov11-May-18 9:07 
QuestionWhy do I need typecast hex? Pin
Vaclav_11-May-18 6:18
memberVaclav_11-May-18 6:18 
AnswerRe: Why do I need typecast hex? Pin
Richard MacCutchan11-May-18 7:25
protectorRichard MacCutchan11-May-18 7:25 
GeneralRe: Why do I need typecast hex? Pin
Vaclav_11-May-18 7:34
memberVaclav_11-May-18 7:34 
Questionconst char [] and char[] difference question Pin
focusdoit10-May-18 15:26
memberfocusdoit10-May-18 15:26 
QuestionRe: const char [] and char[] difference question Pin
David Crow10-May-18 18:22
memberDavid Crow10-May-18 18:22 
AnswerRe: const char [] and char[] difference question Pin
Richard MacCutchan10-May-18 22:25
protectorRichard MacCutchan10-May-18 22:25 

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.


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181207.3 | Last Updated 14 Dec 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid