简体   繁体   English

通过争论之间的区别

[英]Difference between passing an arguement

Hi All I have written two codes大家好我写了两个代码

1. 1.

    #include<iostream>
    using namespace std;
    void swap(int *x, int *y)
    {
        int t;
        t = *x;
        *x = *y;
        *y = t;
    }
    int main()
    {
        int a = 10, b = 20;
        cout << "value of a before swap " << a << endl;
        cout << "value of b before swap " << b << endl;
        swap(&a, &b);
        cout << "value of a after swap " << a << endl;
        cout << "value of b after swap " << b << endl;
        cin.get();

    }

2. 2.

    #include<iostream>
    using namespace std;
    void swap(int *x, int *y)
    {
        int t;
        t = *x;
        *x = *y;
        *y = t;
    }
    int main()
    {
        int a = 10, b = 20;
        cout << "value of a before swap " << a << endl;
        cout << "value of b before swap " << b << endl;
        swap(a, b);
        cout << "value of a after swap " << a << endl;
        cout << "value of b after swap " << b << endl;
        cin.get();

    }

In both cases I am getting same output as value of a before swap 10 value of b before swap 20 value of a after swap 20 value of b after swap 10在这两种情况下,我得到的输出与交换前 10 交换前 b 的值交换前 20 交换后 20 交换后 b 的值交换 10 后的值相同

My First question is Does swap(&a,&b) and swap(a,b) makes no difference to swap function??我的第一个问题是 swap(&a,&b) 和 swap(a,b) 对交换函数没有影响吗??

But when i give same arguments to given below swap function但是当我给下面的交换函数给出相同的参数时

void swap(int &x, int &y)
{
    int t;
    t = x;
    x = y;
    y = t;
}

swap(a,b) gives no issue and work fine but when i pass value as swap(&a,&b) code gives Error error C2665: 'swap': none of the 3 overloads could convert all the argument types Why?? swap(a,b) 没有问题并且工作正常,但是当我将值作为 swap(&a,&b) 传递时,代码给出错误错误 C2665: 'swap': 3 个重载中没有一个可以转换所有参数类型 为什么?

The problem is this evil line:问题是这条邪恶的线:

using namespace std;

In your second example, you're actually calling ::std::swap .在您的第二个示例中,您实际上是在调用::std::swap Since your version of swap takes pointers, you must use the & operator.由于您的swap版本需要指针,您必须使用&运算符。

See Why is “using namespace std;”请参阅为什么“使用命名空间 std;” considered bad practice?被认为是不好的做法?

In the first program there is called your own swap function for pointers.在第一个程序中,调用了您自己的指针交换函数。

In the second program there is called the standard function std::swap for objects of the type int due to unqualified name-lookup and presence of the using directive.在第二个程序中,由于未限定的名称查找和 using 指令的存在,为int类型的对象调用了标准函数std::swap

In the third program (when you supplied a and b ) there is called your own function swap that accepts objects of the type int by reference.在第三个程序中(当您提供ab ),称为您自己的函数 swap,它通过引用接受int类型的对象。 The compiler prefers to use a non-template function if both template and non-template functions are suitable.如果模板和非模板函数都适合,编译器更喜欢使用非模板函数。

But your swap function in the fourth program is not designed to swap pointers.但是您在第四个程序中的交换函数不是为了交换指针而设计的。 So the compiler tries to select a standard swap function std::swap .因此编译器尝试选择标准交换函数std::swap But it is not designed to swap temporary (rvalues) objects.但它并非旨在交换临时(右值)对象。 So the compiler issues an error.所以编译器会报错。

You could call the standard swap function if you introduced intermediate variables that will contain pointers to variables a and b .如果您引入了包含指向变量ab指针的中间变量,则可以调用标准交换函数。

Here is a demonstrative program.这是一个演示程序。

#include<iostream>
using namespace std;

void swap(int &x, int &y)
{
    int t;
    t = x;
    x = y;
    y = t;
}

int main()
{
    int a = 10, b = 20;
    int *pa = &a;
    int *pb = &b;

    cout << "value of *pa before swap " << *pa << endl;
    cout << "value of *pb before swap " << *pb << endl;

    swap( pa, pb); 

    cout << "value of *pa after swap " << *pa << endl;
    cout << "value of (pb after swap " << *pb << endl;

    cin.get();

}

Its output is它的输出是

value of *pa before swap 10
value of *pb before swap 20
value of *pa after swap 20
value of (pb after swap 10  

In this program your own function swap is not called because its parameters are references to objects of the type int but you are calling swap passing objects (pointers) of the type int * .在这个程序中,你自己的函数 swap 没有被调用,因为它的参数是对int类型对象的引用,但你正在调用int *类型的交换传递对象(指针)。

So the standard function std::swap specialized for objects of the type int * is called.因此,专门用于int *类型的对象的标准函数std::swap被调用。

It swaps the pointers themselves not the objects pointed to by the pointers..它交换指针本身而不是指针指向的对象。

There is definitly a difference between 1 and 2 1和2之间肯定有区别

  1. You are taking the address of actually reserved memory devoted to ( holding ) your variables a and that is OK, you can effectively swap their content.您正在获取专门用于(保存)变量 a 的实际保留内存的地址,没关系,您可以有效地交换它们的内容。

  2. You are considering the value of a and b to be a valid address but what I can assure you is that no OS gives you acces of those paricular zones in normal use so the address is wrong and the programs ends with a SEGFAULT and that is NOK.您正在考虑将 a 和 b 的值视为有效地址,但我可以向您保证,在正常使用中,没有操作系统为您提供这些特定区域的访问权限,因此地址是错误的,程序以 SEGFAULT 结尾,即 NOK .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM