Hi Martin,
In your example, you are actually using call by value, which whilst it may work in your example, is probably not the approach you are after.
What Call by Value means is a new object will be created and copied as part of the function call, and destructed when the function returns. In most cases when dealing with objects, you actually want to use Call by Reference, vs. simple datatypes like char, int, float (often refered to as a Plain Old Data structure POD) where call by value is the norm.
In C++, there are actually two ways to do this; one is the newer "references" syntax, and the other is using pointers.
In your example, this would be as simple as adding a "&" to the Serial passed argument, to specify the Serial object should be passed by reference:
#include "mbed.h"
void pass_object(Serial &passed){
passed.printf("I've been passed!");
}
int main() {
Serial pc (USBTX,USBRX); // Local objects
DigitalOut myled(LED1);
pc.printf("passing object... \n\r");
pass_object(pc);
}
In this example, nothing else needs to change (btw, don't confuse the "&" as being the same as "the address of" in pointers).
The alternative is using pointers:
#include "mbed.h"
void pass_object(Serial *passed){
passed->printf("I've been passed!");
}
int main() {
Serial pc (USBTX,USBRX); // Local objects
DigitalOut myled(LED1);
pc.printf("passing object... \n\r");
pass_object(&pc);
}
In this example, the function now accepts a "pointer" to a Serial object. Note in this case, the syntax within the function needs to change (-> from .), and when you call the function, you need to pass a pointer to the object.
The first is probably easier, whilst the second is less likely to confuse C programmers new to C++ :)
Simon
Hi All,
I am working on a program that captures an image form a camera and displays it on a screen. The data received from the camera is too big to fit into an array so I am storing it on an external SRAM IC.
So I want to take a picture, store it in the SRAM IC, retrieve it from the SRAM IC, and display it to the screen.
The screen, camera and SRAM IC all have separate libraries but how can I get them working together? If I create an SRAM object in main() how do I pass that specific object to the camera method to fill with picture data?
This seems like it should be simple but I'm going round in circles with it!
Thanks Martin