簡體   English   中英

Arduino在另一個類的構造函數中將一個對象作為參數傳遞

[英]Arduino passing in one object as a parameter in the constructor of another class

我對此很陌生,所以如果我叫錯名字,請原諒我。 我想做的是將一個類的實例傳遞給另一個類的構造函數。 我知道這些通常是在.h和.cpp文件中完成的,但是對於我運行的代碼來說,它似乎並不在乎,但是我可能是錯的。 除了類defs和構造函數之外,我已經刪除了大部分代碼。

我想在代碼中有一些現有的Thermistor實例(如coldtherm),並將其傳遞給Tempcontroller的構造函數,以便像在printfromthermistor函數中所示的那樣調用Coldtherm。

//Thermistor Class
    class Thermistor
{

  int Thermpin;

public:
  Thermistor(int pin)
  {
  Thermpin = pin;
  }


double TEMPOutput()
  {
  return Thermpin;
  }  
void Update()
  {

  }
};

Thermistor coldtherm(1);

//Tempcontrol Class
class TempController
{

public:

TempController(Thermistor&) //Right here I want to pass in coldtherm to the Tempcontroller and be able to call functions from that class.


void printfromthermistor()
{

  Thermistor.TEMPOutput();
}


};

重復。

引用只能初始化,不能更改。 如所示,在構造函數中使用它意味着必須在構造函數初始化引用成員:

class TempController
{
  Thermistor & member;
public:
  TempController( Thermistor & t ) { member = t; }; // assignment not allowed
  TempController( Thermistor & t ) : member(t) { }; // initialization allowed
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM