实现复制的简单c ++程序输出错误

问题描述 投票:0回答:1
    #include<iostream>
#include<vector>
using namespace std;
class test{
    int a,b;
    public:
    test():a{0},b{0}{}
    test(int,int);
    void copy(test);
    void print();
};
test::test(int a,int b){
    a=a;
    b=b;
}
void test::copy(test obj)
{
    a=obj.a;
    b=obj.b;
}
void test::print()
{
    cout<<test::a<<" <========> "<<b<<endl;
}
int main()
{
    test t1(4,15);
    t1.print();
    test t2=t1;
    t2.print();
}

上面的代码应该打印4 <========> 15 4 <========> 15

但它是打印1733802096 <========> 22093 1733802096 <========> 22093

我没有遇到问题。如果我在构造函数中更改参数名称,它将提供正确的输出。这种行为可能是什么原因?

c++
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.