Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this C++ syntax mean and why does it work?

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

like image 409
greatwolf Avatar asked Sep 03 '10 02:09

greatwolf


People also ask

What is C and why it is used?

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.

How does C function work?

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.

What Is syntax is used for?

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.

What is basic syntax in C programming?

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

What are the basic building blocks of C programming language?

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.

What does the semicolon mean in a C program?

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 −

What is the basic structure of a C program?

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 −


1 Answers

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.

like image 90
rlbond Avatar answered Sep 23 '22 20:09

rlbond