I was looking through the source of OpenDE and I came across some wierd syntax usage of the array indexing operator '[]' on a class. Here's a simplified example to show the syntax:
#include <iostream>
class Point
{
public:
Point() : x(2.8), y(4.2), z(9.5) {}
operator const float *() const
{
return &x;
}
private:
float x, y, z;
};
int main()
{
Point p;
std::cout << "x: " << p[0] << '\n'
<< "y: " << p[1] << '\n'
<< "z: " << p[2];
}
Output:
x: 2.8
y: 4.2
z: 9.5
What's going on here? Why does this syntax work? The Point class contains no overloaded operator []
and here this code is trying to do an automatic conversion to float somewhere.
I've never seen this kind of usage before -- it definitely looks unusual and surprising to say the least.
Thanks
C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
The word “syntax” comes from the Ancient Greek for “coordination” or “ordering together.” In spoken and written language, syntax refers to the set of rules that determines the arrangement of words in a sentence. Along with diction, it is one of the key ways writers convey meaning in a text.
C - Basic Syntax. 1 Tokens in C. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For ... 2 Semicolons. 3 Comments. 4 Identifiers. 5 Keywords. More items
You have seen the basic structure of a C program, so it will be easy to understand other basic building blocks of the C programming language. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
In a C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity. Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminate with the characters */ as shown below −
You have seen the basic structure of a C program, so it will be easy to understand other basic building blocks of the C programming language. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following C statement consists of five tokens −
p
is being converted implicitly into a const float* const
, which points to x
. So *p
is x
, *(p+1)
is y
, and so on.
It's a pretty weird idea (and confusing!) to do it this way, of course. It's usually preferable to store x, y, and z in an array and have a function to get the entire array if they really want to do things this way.
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