0

I'm reading the two wire library in Arduino libraries and in the C++ source code I found different methods which I don't know what they mean, in the following:

// Initialize Class Variables //////////////////////////////////////////////////

uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
uint8_t TwoWire::rxBufferIndex = 0;
uint8_t TwoWire::rxBufferLength = 0;

uint8_t TwoWire::txAddress = 0;
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
uint8_t TwoWire::txBufferIndex = 0;
uint8_t TwoWire::txBufferLength = 0;

uint8_t TwoWire::transmitting = 0;
void (*TwoWire::user_onRequest)(void);
void (*TwoWire::user_onReceive)(int);


// Public Methods //////////////////////////////////////////////////////////////
void TwoWire::begin(void)
{
  rxBufferIndex = 0;
  rxBufferLength = 0;

  txBufferIndex = 0;
  txBufferLength = 0;

  twi_init();
}

void TwoWire::begin(uint8_t address)
{
  twi_setAddress(address);
  twi_attachSlaveTxEvent(onRequestService);
  twi_attachSlaveRxEvent(onReceiveService);
  begin();
}

void TwoWire::begin(int address)
{
  begin((uint8_t)address);
}

My first question, regarding to class variables:

  1. Why has the author connected the variables to the class with scope-resolution operator "::"?
  2. I want to understand the last two lines of class variables, I know they are function pointers, but I want to know more details and benefits of function pointers?

My second question, regarding to public methods:

  1. There are three versions of the "begin" function, can someone explain why?
  2. Why is begin(); called inside the second function declaration?
1

Why they connect the variables to the class with scope-resolution operator "::"?

Those are static variables. They don't exist within an instantiated class instance since they are common to all class instances - so they have to be defined elsewhere, and for that you need the full "class path" for them to associate them with the class itself.

I want to understand the last two lines of class variables, I know they are function pointers, but I want to know more details and benefits of function pointers?

Yes, they are function pointers. They are used to store the address of functions the user supplies at runtime. Without them it is very hard for the user to tell the class which of his own functions should be called for various events.

There are three versions of the "begin" function, can someone explain why?

This is called function overloading. It allows you to have the same function but provide different combinations of parameters. In this case one with no functions (in master mode) and two with different numeric parameters (for slave mode) with those numeric parameters in different storage formats.

Why begin(); is called inside the second function declaration?

It saves having to duplicate all the code from the other begin() function within that function. You call the function with a numeric parameter, it does a little bit of "extra" work, then it calls the "common" begin code from the other begin() function. Code duplication is generally a bad thing since any changes you make have to be made in multiple places. This keeps the common code in one location, and the most convenient place for that is the begin() function that takes no parameters.

  • Guys, first of all thank you very much for Mr Code Gorilla for correcting my English it's really a honor for me that a friend in the technical field is interested to help me with my English skills :), and secondly, thanks to Mr Majenko for answering my questions. I'm really thrilled that one help me with my English and the other one helped me with my technical topic. I understood some answers and others not so quite, so I need more time to understand and comment again for the answers. – R1S8K Apr 28 '17 at 14:54
  • There are some complex things there you are looking at. May help: learncpp.com/cpp-tutorial/811-static-member-variables - tutorialspoint.com/cplusplus/cpp_overloading.htm – Majenko Apr 28 '17 at 14:55
  • 1. For the fisrt answer, I understood half of your answer as static variables are shared and that's it but I didn't understand the second half, "so they have to be defined elsewhere, and for that you need the full "class path" for them to associate them with the class itself." Do you mean by full path is to define their type and connect them with the class name? I think yes, and also I understood in the library that the author is defining them at the beginning and before class definition with the class name. OK this point is now obvious to me :) – R1S8K Apr 28 '17 at 17:50
  • @PerchEagle With a static variable the type is defined in the class, but the storage space for that variable has to be manually defined, and that is what those entries are. A normal variable has the storage space allocated in a class instance when it is created (e.g., Servo myServo; allocates the space for the variables within the Servo class). Static variables can't do that since the storage space isn't specific to any one object, but to all objects - so you have to manually create that storage space. And you do that by defining the variables in a source file. The variable's name is ... – Majenko Apr 28 '17 at 17:53
  • ... <ClassName>::<VariableName>. You need the <ClassName>:: bit so that the compiler knows you mean the variable in that class, not some other unrelated variable. – Majenko Apr 28 '17 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.