my stuff

what's new with me

Monday, December 20, 2004

Learning more C++

A few years ago I bought Bjarne Stroustrup's "The C++ Programming Language" with the intent of soaking it into my brain, but got a few chapters in before being distracted by something shiny. This break I've come back at it in earnest, and I'm glad I did. I've learned a lot, and I've been coding in C++ for a while. For instance, I found the following bit of code especially intriguing.

#include <iostream>

double& lvalue_me() {
    static double secret_double;
    return secret_double;
}

int main() {
    using std::cout;

    cout << lvalue_me() << endl;

    lvalue_me() = 1.0;

    cout << lvalue_me() << endl;

    return 0;
}


In particular I just find the fact that by returning a reference, a function can be used as an lvalue. It makes perfect sense of course, I just never thought of it that way before.

Just thought I'd share....

0 Comments:

Post a Comment

<< Home