1186324352 发表于 2014-1-15 23:04:50

C++第二十五课的问题

关于第二十五课的示例,小甲鱼老师说的看懂了,倒是有一个地方迷糊了。
仿小甲鱼老师的代码:#include <iostream>
#include <string>

class Complex
{
public:
    Complex();
    Complex(int x,int y);
    Complex add_Complex(Complex &c);
        void print();
private:
    int a;
    int b;
};
Complex::Complex()
{
    a=0;
    b=0;
}
Complex::Complex(int x,int y)
{
    a=x;
    b=y;
}
Complex Complex::add_Complex(Complex &c)
{
    return Complex(a+c.a,b+c.b);
}
void Complex::print()
{
        std::cout<<"a:"<<a<<"   b:"<<b<<std::endl;       
}
int main()
{
    Complex c1(3,5);
    Complex c2(8,-1);
    Complex c3;
    c3=c1.add_Complex(c2);
    c3.print();
    return 0;
}自己又改了改#include <iostream>
#include <string>

class Complex
{
public:
    Complex();
    Complex(int x,int y);
    Complex add_Complex(Complex c);
        void print();
private:
    int a;
    int b;
};
Complex::Complex()
{
    a=0;
    b=0;
}
Complex::Complex(int x,int y)
{
    a=x;
    b=y;
}
Complex Complex::add_Complex(Complex c)
{
    return Complex(a+c.a,b+c.b);
}
void Complex::print()
{
        std::cout<<"a:"<<a<<"   b:"<<b<<std::endl;       
}
int main()
{
    Complex c1(3,5);
    Complex c2(8,-1);
    Complex c3;
    c3=c1.add_Complex(c2);
    c3.print();
    return 0;
}
结果是一样的,那么方法里面有没有&到底有什么区别呢?
页: [1]
查看完整版本: C++第二十五课的问题