【问题标题】:How to access data type of a class declared in one class into another class (both in different translation unit)?如何将一个类中声明的类的数据类型访问到另一个类(都在不同的翻译单元中)?
【发布时间】:2016-08-16 09:31:42
【问题描述】:

我有 5 个文件。 (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

然后我将其实现为

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

终于主为

// 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 类中? 我曾尝试使用前向声明,但惨遭失败。

【问题讨论】:

  • 在您的 main 文件中,我发现 #include "A.cpp" 很奇怪...您的意思是 #include "A.hpp" 吗?
  • @WhiZTiM 我这样做是为了不用做 g++ --std=c++11 A.cpp main.cpp .. 这样我就可以直接做 g++ --std= c++11 main.cpp
  • 这是一种奇怪的做法。天生脆弱。这就像坐在一桶火药上...... Kaboom !!。根据 C++ 的 ODR,如果您在另一个翻译单元中执行相同的 #include,您将遇到多个定义的问题;
  • @pokche 不要那样做:这是一个糟糕的主意。
  • 我不知道我会不会走那么远。我认为“让我们在秋天入侵俄罗斯”仍然成功。

标签: c++


【解决方案1】:

打开你的 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 成员的引用,而不是对其进行复制。

请注意,该参数声明为对sconst 实例的引用,因为f1() 不需要修改它。如果是这样,只需将其作为非常量引用传递:

int B::f1(s &my_struct){ 

现在,f1() 将能够修改 my_struct,并最终修改传递给它的 A 类的这个私有实例。

附:这与不同的翻译单元无关。无论整件事是在一个翻译单元中,还是分成六个翻译单元,类和引用的工作方式都是一样的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 2012-05-18
    相关资源
    最近更新 更多