如何将在一个类中声明的类的数据类型访问到另一个类(两者都在不同的翻译单元中)
How to access data type of a class declared in one class into another class (both in different translation unit)?
我有5个文件。(1. A.hpp, A.cpp: 2。b.h hpp, b.p p: 3 main.cpp)
// A.hpp
#ifndef MY_CLASS_A
#define MY_CLASS_A
#include <iostream>
class B; // forward declaration so that I could do B * b;
struct s {
int x;
double y;
};
class A{
s my_struct;
int size;
B * b;
public:
A(int, double, int);
void f1(s);
void f2(); // this function calls B's f1 function
s get_struct();
int get_size();
void print();
};
#endif
然后我将其实现为
// A.cpp
#include "A.hpp"
#include "B.hpp"
A::A(int x_, double y_, int size_):my_struct({x_, y_}), size(size_){}
void A::f1(s s_){
// do stuff
s_.x = 5;
s_.y = 51.99;
}
void A::f2(){
int val;
// Here I am calling B's f1 function
val = b->f1(my_struct);
}
s A::get_struct(){
return my_struct;
}
int A::get_size(){
return size;
}
void A::print(){
std::cout << " ----- " << std::endl;
std::cout << "x = " << my_struct.x << std::endl;
std::cout << "y = " << my_struct.y << std::endl;
std::cout << "size = " << size << std::endl;
std::cout << " ----- " << std::endl;
}
然后是B
//B.hpp
#ifndef MY_CLASS_B
#define MY_CASS_B
#include "A.hpp" // I placed here A.hpp because I am
// trying to use A's struct type
class A;
class B{
public:
int f1(s); // A's struct use here to get struct by value
};
#endif
及其作为
的实现// B.cpp
#include "B.hpp"
// used A's struct here again
int B::f1(s my_struct){
std::cout << "*****" << std::endl;
std::cout << my_struct.x << std::endl;
std::cout << my_struct.y << std::endl;
std::cout << "*****" << std::endl;
}
finally main as
// main.cpp
// As per comment I should place #include "A.hpp" here
#include "A.cpp"
int main(){
A a(4,9.9, 5);
a.print();
return 0;
}
我的主要问题是如何将A类中声明的结构访问到B类中?我尝试过使用forward declaration,但是失败了。
打开c++课本,翻到指针和引用那一章,再读一遍。
"在类A中声明的结构体"是my_struct
,它是private
类成员。
要在其他地方访问它,需要通过引用传递它。
int B::f1(const s &my_struct){
std::cout << "*****" << std::endl;
std::cout << my_struct.x << std::endl;
std::cout << my_struct.y << std::endl;
std::cout << "*****" << std::endl;
}
现在,当你从A
调用这个:
val = b->f1(my_struct);
现在将传递对my_struct
成员的引用,而不是复制它。
注意,这个参数被声明为对s
的const
实例的引用,因为f1()
不需要修改它。如果存在,只需将其作为非const引用传递:
int B::f1(s &my_struct){
现在,f1()
将能够修改my_struct
,并最终修改传递给A
的类的私有实例。
注:这与不同的翻译单位无关。无论整个事情是在一个翻译单元中,还是被分成六个翻译单元,类和引用的工作方式都是一样的。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 如何在 c++ 中实现 avl 树,每个节点都是另一个 avl 树
- 如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
- 带大小参数和不带大小参数的"运算符删除":当两者都可用时,选择哪一个?
- 两种模板类型的sizeof,当两者都是从一个基派生时
- 如果其中一个为真,则 && 结束循环,而不是如果两者都为真
- 如何编写两个初始化操作(一个作为默认初始化,另一个作为用户输入)?两者都是类的构造函数C++
- 如果在另一个库和链接到此库的应用程序中都使用了仅标头的库,是否存在任何问题
- 如何有效地设计一种方法,使每个条件都依赖于另一个条件
- 从一个模板类转换到另一个(两者都派生自同一基类)
- 如何从文件中的类调用,以使用着色器显示在另一个包含gl代码的文件中(其中两个文件都在jni文件夹中)
- 复制链表,每个节点都有随机链接,每个节点都有一个变量,随机指向列表中的另一个节点
- 在另一个类中使用一个类的变量(都是从抽象类定义的)
- 如果我把这些都放到另一个函数里,是不是很好
- 如何从另一个文件中调用与另一个文件中同名的函数(当两者都包含时)
- 如何将在一个类中声明的类的数据类型访问到另一个类(两者都在不同的翻译单元中)