Boost logo

Ublas :

Subject: Re: [ublas] define a matrix in my class
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2010-02-02 10:22:13


David Bellot wrote:

> I'm lost. What's the trick here ?
> (and I thought I was good in C++ ;-) )
>

The trick is to declare the variable qt static, which makes its lifetime the
same of that of the program. There will be exactly one instance of that
object, and you don't need to construct the class to access it. You've
probably seen

struct B {
  static const bool value = true;
};

which means you can do

std::cout << B::value << std::endl;
B b;
std::cout << b.value << std::endl;

now, suppose you just wanted to replace this bool with a matrix, say

struct C {
  static const ublas::identity_matrix<double> qt =
    ublas::identity_matrix<double>(3);
};

the compiler will complain about constructors not being allowed in constant
expressions. The initialization of the static data member needs to be out-
of-class, i.e., in the syntax of the example I sent.

HtH,
Cheers,

Rutger