这是可能的这种方式或另一种方式将类传递给另一个类作为参数?关键是我想将许多变量(向量,整数,字符串)从一个类传递到另一个类,以便可以使用所有变量。这有可能吗?如何将类传递给类可以在C++中使用吗?
class classA{
public:
int a;
ClassA(){
int a =11;
int b =11;
int c =11;
int d =11;
}
void function(){
a++;
b++;
c++;
d++;
}
};
class classB{
public:
int x;
ClassB(class class1){
int x = 21;
}
void function1(){
x= x+class1.a+class1.b+class1.c+class1.d;
}
void function2(){
x= x+class1.a+class1.b+class1.c+class1.d;
}
void function3(){
x= x+class1.a+class1.b+class1.c+class1.d;
}
};
int main{
ClassA class1;
ClassB class2;
class1.function();
class2.function1(&class1);
class2.function2(&class1);
class2.function3(&class1);
return class2.x;
}
您应该熟悉通过值,引用和指针传递参数的概念。 –
我建议读入C++的面向对象编程 –
不要试图通过试错来学习C++,那真的不行。相反,从一本好书中系统地学习它。 –