I found a crazy mistake in my code.
I had written the following line:
GLfloat* points = new GLfloat(1024);
rather than
GLfloat* points = new GLfloat[1024];
I only just noticed it. My code has compiled and run a few times before I noticed the error. I realize that this is by fluke, but my question is what does the line I originally had do?
I notice that it looks kind of like the creation of a class using a pointer to allocated memory. Does it create a single GLfloat on the heap with the initial value of 1024.0
? If this is true, why is it valid syntax? (GLfloat is not a class, is it?)
Parentheses are used to enclose numbers, words, phrases, sentences, letters, symbols, and other items while brackets are used to enclose information that is inserted into a quote as well as parenthetical materials within the parentheses.
Write a class that holds a string. Overload operator() to return the substring that starts at the index of the first parameter. The length of the substring should be defined by the second parameter. Hint: You can use std::string::substr to get a substring of a std::string.
In general, in programming languages, "extra" parentheses implies that they are not changing the syntactical parsing order or meaning.
Parenthesis are used in an arrow function to return an object. Parenthesis are used to group multiline of codes on JavaScript return statement so to prevent semicolon inserted automatically in the wrong place.
what does the line I originally had do?
GLfloat* points = new GLfloat(1024);
Let us try to replace GLfloat
with int
, you will see that if GLFloat
is a type similar to int
or float
, then you will have the following:
int * points = new int(1024);
The above statement means that you are creating a pointer to an int
with initial value being 1024
. So in your case, it means creating a pointer points
to a variable with type being GLfloat
and initial value as 1024
.
It is equivalent to write the following in a condensed version:
int * points = new int;
*points = 1024;
See here for more explanation.
GLfloat
is an OpenGL alias for float
(i.e., typedef float GLfloat;
). Consequently the code:
GLfloat* points = new GLfloat(1024);
Is equivalent to:
float* points = new float(1024);
Which allocates a floating point number and initializes it to 1024.0
and assigns its address to pointer points
.
Yes, you are creating a single GLFloat
on the heap initialized to 1024.0. You can initialize primitives with the same syntax as classes. e.g.
int i(10);
Would create an int on the stack initialized to 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With