简体   繁体   English

C ++将结构从一类传递到另一类

[英]C++ pass struct from from one class to another class

I have two classes A and B defined in Ah, A.cpp and Bh, B.cpp respectively. 我分别在Ah,A.cpp和Bh,B.cpp中定义了两个类A和B。 Class A has a structure that I want to use in a function of Class B. Because Class B is included in Class A, I can't include class A in Class B as it will lead to circular dependency. 类A具有要在类B的函数中使用的结构。由于类B包含在类A中,因此我不能在类B中包含类A,因为这会导致循环依赖。 Code for all the files are given below: Ah 下面给出了所有文件的代码:

#ifndef _A_H
#define _A_H
#include B.h
namespace common {
    class A {
    public:
        static struct strctOfA {
           float p1 = 2;
        } structA;
        void functionOfA();
    };
}
#endif // !_A_H

A.cpp 丙型肝炎

#include A.h
using namespace common;
A::functionOfA() {
    B b;
    b.functionOfB(structA);
}

Bh h

#ifndef _B_H
#define _B_H
namespace common {
    class B {
    public:
         functionOfB(??);
    };
}
#endif // !_B_H

B.cpp 丙型肝炎

#include B.h
using namespace common;
B::functionOfB(??) {
        // Want to use structA here;        
}

I looked into StackOverflow and found a thread quite close to my problem, however, they it either doesn't explains the answer or I am not able to understand their solution. 我调查了StackOverflow,发现一个线程非常接近我的问题,但是,他们要么无法解释答案,要么我无法理解他们的解决方案。 Please help me with this. 请帮我解决一下这个。

Thanks 谢谢

Update Thanks IV for pointing out the mistake and providing me the explanation. 更新感谢IV指出错误并提供了解释。 Updating the code worked for me. 更新代码对我有用。 Thanks everyone for providing additional knowledge to my C/C++ coding skills. 感谢每个人都为我的C / C ++编码技能提供了更多知识。 :) :)

The solution is simple, what you wrote is a bad habit. 解决方法很简单,您写的是一个坏习惯。

There is no reason to include Bh in Ah because you don't use any part of Bh in the Ah file. 没有理由在Ah中包含Bh,因为您没有在Ah文件中使用Bh的任何部分。 A better way to implement it is to include Bh in A.cpp and not in the header file. 一种更好的实现方法是在A.cpp中而不是在头文件中包含Bh。

It will also solve the cycle of includes you have.. 它还将解决包含问题的循环。

In general, it is recommended to include files in .cpp file and NOT in header files when you don't use functions/objects of the included file in the header file :) 通常,如果您不在头文件中使用包含文件的功能/对象,建议将文件包括在.cpp文件中,而不是包含在头文件中:)

In addition, you should use #pragma once for Windows or ifndef for anything else in order to be safe of conflicts 此外,对于Windows,您应该一次使用#pragma,对于其他任何情况都应使用#n

Ah

#ifndef _A_H
#define _A_H
namespace common {
    class A {
    public:
        static struct strctOfA {
           float p1 = 2;
        } structA;
    }
}
#endif

A.cpp 丙型肝炎

#include A.h
#include B.h
using namespace common;
class A {
    B b;
    b.functionOfB(structA);
}

NOTE: from B.cpp include Ah 注意:从B.cpp包括Ah

That can be done with forward-declarations, unless you abolutely must pass it by value. 可以使用前向声明来完成,除非您绝对必须按值传递它。 But I can't think of a scenario where this wouldn't work with references. 但是我无法想到这种情况下无法使用引用。

Ah

#ifndef A_H_
#define A_H_

struct B;
struct A {void f(B&);};

#endif

A.cpp 丙型肝炎

#include "B.h"
void A::f(B & value) {/* do something */ };

Bh h

#ifndef B_H_
#define B_H_


struct A;
struct B {void f(B&);};

#endif

B.cpp 丙型肝炎

#include "A.h"
void B::f(A & value) {/* do something */ };

If you need to have a value of B in A with only a forward-declaration you can put it in a pointer, ie use a std::unique_ptr. 如果只需要前向声明就需要A中的B值,则可以将其放在指针中,即使用std :: unique_ptr。

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

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