overload - pass by value

  • [pass by left-value refernece]
  • [pass by right-value refernece]
点击查看代码
#include<iostream>
#include<vector>

using namespace std;

void print(int &&param) {
    cout << "param is a rignt-value reference:"
    << "param = " << param<< endl;
}
/*
// error
void print(int param) {
    cout << "param is a left-value:"
    << "param = " << param<< endl;
}
*/
void print(int &param) {
    int& p = param;
    p = 100;
    cout << "param is a left-value reference:"
    << "param = " << param<< endl;
}

int main() {
    int a = 3;
    cout << "a = " << a << endl;
    print(a);// call "print(int &)"
    cout << "a = " << a << endl;
    cout << "" << endl;
    print(std::move(a));// call "print(int &&)"
    cout << "" << endl;  
    print(1000);// call "print(int &&)"
    cout << "" << endl;
    return 0;
}
posted @ 2022-07-24 18:13  locker_10086  阅读(13)  评论(0编辑  收藏  举报