简体   繁体   English

如何将在一个类中声明的类的数据类型访问到另一个类中(两者都在不同的翻译单元中)?

[英]How to access data type of a class declared in one class into another class (both in different translation unit)?

I have 5 files. 我有5个档案。 (1. A.hpp, A.cpp : 2. B.hpp, B.cpp : 3 main.cpp) (1. A.hpp,A.cpp:2。B.hpp,B.cpp: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

Then I have its implementation as 然后我将其实现为

// 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;
}

Then I have B 那我有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

and its implementation as 及其实现为

// 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;
}

My main question is how can I access the struct declared in class A into class B? 我的主要问题是如何将A类中声明的结构访问B类? I have tried to use forward declaration by failed miserably. 我试图通过失败来使用前向声明。

Open your C++ book to the chapter on pointers and references, and read that chapter again. 将您的C ++书打开到有关指针和引用的章节,然后再次阅读该章节。

"The struct declared in class A" is my_struct , which is a private class member. “在类A中声明的结构”是my_struct ,它是private类成员。

To have it accessible elsewhere, you need to pass it by reference. 要使其在其他位置可访问,您需要通过引用将其传递。

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;
}

Now, when you invoke this from A : 现在,当您从A调用它时:

val = b->f1(my_struct);

This will now pass a reference to the my_struct member, instead of making a copy of it. 现在,这将传递对my_struct成员的引用,而不是对其进行复制。

Note that the parameter is declared as a reference to a const instance of s , because f1() does not need to modify it. 注意,该参数被声明为对sconst实例的引用,因为f1()不需要修改它。 If it does, simply pass it as a non-const reference: 如果是这样,只需将其作为非常量引用传递即可:

int B::f1(s &my_struct){ 

Now, f1() will be able to modify my_struct , and end up modifying this private instance of A 's class, that was passed to it. 现在, f1()将能够修改my_struct ,并最终修改传递给它的A类的私有实例。

PS This has nothing to do with different translation units. PS这与不同的翻译单位无关。 Whether this whole thing is in one translation unit, or is split across half a dozen of them, classes and references work the same way. 无论是将整个内容放在一个翻译单元中,还是分成六个翻译单元,类和引用的工作方式都相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM