in reply to Re^3: returning an object in XS
in thread returning an object in XS
It's a variable declaration with an initialiser.
$ cat a.cpp #include <stdio.h> struct Point { Point(int a_x, int a_y) : x(a_x), y(a_y) {} int x; int y; }; int main() { Point point(5,6); printf("%d,%d\n", point.x, point.y); return 0; } $ g++ -o a a.cpp && a 5,6
It's like new, but the object is allocated on the stack instead of the heap, and the variable is a reference instead of a pointer. It's the object equivalent of
int i = 5;
which can also be written as
int i(5);
which is what allowed me to write
Point(int a_x, int a_y) : x(a_x), y(a_y) {}
instead of
Point(int a_x, int a_y) { x = a_x; y = a_y; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: returning an object in XS
by BrowserUk (Patriarch) on Feb 29, 2012 at 19:18 UTC | |
by bipham (Novice) on Feb 29, 2012 at 21:11 UTC | |
by BrowserUk (Patriarch) on Feb 29, 2012 at 21:23 UTC |