So, I have these two functions ref() and pointy() that respectively create a local reference and pointer to a locally-defined int:
#include <iostream>
int& ref() {
int knuckles = 6;
int &chuckles = knuckles;
return chuckles;
};
int* pointy() {
int buckles = 8;
return &buckles;
};
int main(int argc, char **argv) {
int a = ref(), *b = pointy();
int c = 14, d = 20;
std::cout << a << ' ' << *b << ' ' << c+d;
};
The code compiles fine, and it gives a warning about returning the address of local variable buckles, but what concerns me is that it doesn’t say anything about ref() returning a reference to knuckles.
Is my compiler (g++ via MinGW, if it makes a difference) just sleeping on the job? Is there something about references that keeps the referent from going out of scope? Or is my reference syntax just bad?
Much obliged!
Your two test cases aren’t parallel. This program:
Produces these warnings: