OpenGL 関数の話はすこしお休みして、一時変数の話をしようと思います。哥貴分とそんな話をしたので、ぼくが忘れてしまわないうちに。

#include <iostream>

class A {
int x_;
public:
A()
: x_(123)
{
std::cout << "A::(construct)" << std::endl;
}

~A() {
std::cout << "A::(destruct)" << std::endl;
}

friend std::ostream& operator<<(std::ostream& out, const A& rhs) {
return out << rhs.x_;
}
};

const A& pass_through(const A& ref) {
return ref;
}

int main(int, char* [])
{
const A& ref = pass_through(A());
std::cout << ref << std::endl;
return 0;
}

いや、たぶん、動きますけどね(デストラクタが呼ばれても、おそらくスタックはそのままなので)。こういうコードを書くのはやめましょうね。ていうか、ヤメロ。ちなみに実行結果は(gcc 3.2.1 on Solaris 8 では)、

A::(construct)
A::(destruct)
123