【发布时间】:2013-07-20 14:53:00
【问题描述】:
我是 C++ 新手,我一直在尝试了解 C++ 中的类。最近我试用了这个程序,它没有返回整数 9,而是返回了一些垃圾值。有人可以帮帮我吗
#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
class abc;
class xyz
{
int a;
public:
friend int add(xyz, abc);
friend void setval(xyz, int, abc, int);
};
class abc
{
int b;
public:
friend int add(xyz, abc);
friend void setval(xyz, int, abc, int);
};
int add(xyz V1, abc V2)
{ return (V1.a + V2.b);}
void setval(xyz v1, int v11, abc v2, int v22)
{v1.a = v11; v2.b = v22; }
int main()
{
xyz A;
abc B;
setval(A, 4, B, 5);
cout<<add(A, B)<<endl;
return(0);
}
【问题讨论】:
-
看代码有点痛苦..为什么类名是
xyz..abc... -
@AnnieKim 我只是在尝试我正在学习的概念,所以只是一个随机代码,没有具体原因。
-
@Sankalp 感谢它解决问题的答案,我将进一步研究通过引用传递值。
-
是的..我知道..这3个答案是相似的,你可以选择一个作为答案,如果你认为他们有帮助,可以投票给其他人..
标签: c++ class oop friend-function