简体   繁体   中英

C++ function prototype declared in class definition in .h file but function not defined in .cpp

I am trying to learn C++ by looking at actual code that works in the Arduino IDE.

I ran across a function prototype (readSensor) declared in a class in the .h file that was not defined in the .cpp file.

class BME280Class
{
 private:
   uint16_t readSensor(uint8_t command);

the function readSensor() was never called. The compiler compiled and produced an executable which ran.

Can one have prototypes in .h files not defined in .cpp files and the compiler not care unless it is called? It probably is not good practice is it?

Yes you can have prototypes declared and not defined.

A very good example is making a class not copyable by declaring the copy constructor and assignment private but not defining them. This is good practice.

eg

class Foo {
   private:
     Foo( const Foo& other ); // non construction-copyable - not defined
     Foo& operator=( const Foo& ); // non copyable - not defined
 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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