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

时间:2015-12-01 14:28:18

标签: c++

我有一个简单的问题。你会如何将类的实例传递给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中编译它时,我在SentenceSelection.h中得到了以下错误“期望构造函数,析构函数或类型转换'('token'之后的行 - ”SentenceSelection :: SentenceSelection(TestBenchGui test)“。

4 个答案:

答案 0 :(得分:0)

  

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

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

B(A *instanceA);

答案 1 :(得分:0)

我相信你所拥有的代码几乎就是你所要求的,但是有一些语法错误可能会让你退出编译(从而导致一些混乱)。下面是我放在单个文件(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

希望这有帮助。

答案 2 :(得分:0)

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

答案 3 :(得分:-1)

我严格回答你的问题: 如果要在构造函数上传递实例,最佳实践将作为参考传递:

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

};

但是在你的例子中,你有一个带有交叉引用的类,这是另一种情况