如何通过构造函数将一个类的实例传递给另一个类



我有一个简单的问题。在c++中,如何将类的实例传递给另一个类的构造函数?我有C语言的经验,但我在C++语法方面很吃力。

例如,假设我有类A,并希望将其实例传递给类B。

A.h
class A {
   A();
   virtual ~A(); 
   public:
   B *newb;   
}

A.cpp
A::A() {
   newb = new B(this);
}
b.h
class B {
  B(A *instanceA);
  virtual ~B(); 

}

有人能给我举个简单的例子吗?非常感谢。

编辑:我在当前的代码中尝试了这个概念,但不断出现错误。对不起,我想确保我使用了正确的原则。这是我目前正在处理的代码片段

     //SentencSelection.h
        class SentenceSelection
        {   
        public:
            SentenceSelection(TestBenchGui *test);
            virtual ~SentenceSelection();
               //do stuff
        };
     //SentencSelection.cpp
        #include <iostream>
        #include "SentenceSelection.h"
        using namespace std;
        SentenceSelection::SentenceSelection(TestBenchGui *test)
        {
             //do stuff
        }
        SentenceSelection::~SentenceSelection()
        {
        }
    //TestBenchGui.h
     #include "SentenceSelection.h"

    class TestBenchGui
    {
    public:
        TestBenchGui();
        virtual ~TestBenchGui();

    private:
        SentenceSelection *selection;
    };
    //TestBenchGui.cpp
    #include "TestBenchGui.h"
    #include "SentenceSelection.h"

    using namespace std;

    TestBenchGui::TestBenchGui()
    {
        selection = new SentenceSelection(this);
    }
    TestBenchGui::~TestBenchGui()
    {
    }

当我在eclipse中编译它时,我得到了以下错误"预期的构造函数,析构函数,或类型转换之前'('token)"的行-"句子选择::句子选择(TestBenchGui测试)"。

我相信您的代码基本上满足了您的要求,但有几个语法错误可能会阻碍您的编译(从而导致一些混乱)。下面是我放在一个文件(main.cpp)中并能够编译的代码。我添加了一些评论和打印声明,以强调宣言和实施之间的区别:

#include <iostream>
using std::cout;
using std::endl;
// forward declaration so we can refer to it in A
class B;
// define A's interface
class A { 
public:
     // member variables
     B *newb;   
     // constructor declaration
     A();
     // destructor declaration
     virtual ~A();
};  // note the semicolon that ends the definition of A as a class
class B { 
public:
    // B constructor implementation
    B(A *instanceA){cout << "I am creating a B with a pointer to an A at " << instanceA << endl;}
    // B destructor implementation
    virtual ~B(){cout << "Destructor of B" << endl;}
};  // note the semicolon that ends the definition of B as a class
// A constructor implementation
A::A(){
     cout << "I am creating an A at " << this << ", allocating a B on the heap..." << endl;
    newb = new B(this);
}
// A destructor implementation
A::~A(){
    cout << "Destructor of A, deleting my B...." << endl;
    delete newb;
}
int main(){
    A an_a;
}

我的系统运行上述程序的输出:

I am creating an A at 0x7fff64884ba0, allocating a B on the heap...
I am creating a B with a pointer to an A at 0x7fff64884ba0
Destructor of A, deleting my B....
Destructor of B

希望这能有所帮助。

例如,假设我有类A,并希望将其实例传递给类B。

如果您打算传递一个指向实例的指针,那么您已经想好了。您声明的构造函数将A*作为参数:

B(A *instanceA);
#include <iostream>
using namespace std;
class B
{
 public:
 B(){};
 int x;
 ~B(){};
};

class A
{
  public:
  A(){}; // default constructor
  A(B& b) // constructor taking B
  {
    y = b.x;
  }
  ~A(){};
  int y;
};


   int main()
   {
     B myB;
     myB.x = 69;
     A myA(myB);
     cout << myA.y << endl;
     return 0;
   }

我严格回答您的问题:如果你想在构造函数上传递一个实例,最好的做法是作为引用传递:

 class CA{
     CB &b;
   public:
      CA( CB &p_b) : b(p_b){};
      virtual ~CA();

};

但在您的示例中,您有一个具有交叉引用的类,这是

的另一种情况

相关内容

最新更新