views:

60

answers:

4
+1  Q: 

Constant member

I have structure defined in some header (D3DXVECTOR3)

How can I declare:

  1. static member in the class of that type and initialize it?
  2. maybe constant member of that type and init it?

when i use some constructor i get error only integral can be initialized.

+1  A: 

Use initializer list to initialize const members.

For example

struct demo
{

    const int x;

    demo():x(10)
    {
        //some code
    }

};

As far as initializing static members(inside the class) is concerned (you can initialize them inside the class only if they are const-static integers)

For example

struct abc{

     static const int k=10; //fine
     static int p=10; //Invalid
     static const double r =2.3 //Invalid
      // ......

   };

  const int abc::k ; //Definition
Prasoon Saurav
as i understood I have to write some wrapper around that struct?it is rather complex because of structure complexityand how to initialize them if they are const static thnx.
TGadfly
+1  A: 

You can't just modify the already-existing struct. That would be a redefinition. Not fun stuff.

You can wrap it like TGadfly suggested.

ProgramMax
+1  A: 

To have a static member of non-int type, use the following construct:

class foo {
    // Declarations:
    static Type1 field1; // or
    static Type2 const field1;
};

// Definitions and initializations:
Type1 foo::field1 = value1;
Type2 const foo::field2 = value2;
Konrad Rudolph
A: 

in header file I declared

class Bar_class
{
  static const D3DXVECTOR3 foo;
}

in cpp file I wrote

const D3DXVECTOR3 Bar_class::foo =D3DXVECTOR3 (1,1,1);
TGadfly
So does this solve the problem?
Konrad Rudolph
Yes this solved the problem
TGadfly
Then mark it as the accepted answer to show that the question is resolved.
Konrad Rudolph
it says in two 2 days. thanks to all for help
TGadfly