Initializing a static const member array of a class type

Discussion in 'C++' started by Steven T. Hatton, Apr 19, 2004.

  1. Is there a way to initialize an member array of user defined objects that is
    declared static const?

    For example:

    const vec3f I(1,0,0);
    const vec3f j(0,1,0);

    class corner_functor
    {
    public:
    corner_functor();

    ~corner_functor();
    static const Vec3f dir[5] = {-I, J, I, -J, -I};//This doesn't work
    private:
    int index;

    };


    I also tried some variations of this:

    const SbVec3f* corner_functor::dir =
    {
    -I, J, I, -J, -I
    };


    --
    STH
    Hatton's Law: "There is only One inviolable Law"
    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
    Mozilla: http://www.mozilla.org
    Steven T. Hatton, Apr 19, 2004
    #1
    1. Advertising

  2. Steven T. Hatton

    Siemel Naran Guest

    "Steven T. Hatton" <> wrote in message

    > Is there a way to initialize an member array of user defined objects that

    is
    > declared static const?
    >
    > For example:
    >
    > const vec3f I(1,0,0);
    > const vec3f j(0,1,0);
    >
    > class corner_functor
    > {
    > public:
    > corner_functor();
    >
    > ~corner_functor();
    > static const Vec3f dir[5] = {-I, J, I, -J, -I};//This doesn't work
    > private:
    > int index;
    >
    > };


    Only const int, long, short, char, bool, and integral constants in general
    can be initialized in the class. Try,

    class corner_functor
    {
    ...
    static const Vec3f dir[5];
    };


    const Vec3f corner_functor::dir[5] = {-I, J, I, -J, -I};

    The above line should occur in the cpp file. You can also define I and J in
    the same cpp file.


    Note that the following is undefined in ANSI C++

    // header.h
    extern const Vec3f I;
    extern const Vec3f J;

    // functor.h
    defines class corner_functor;

    // functor.cpp
    #include "header.h"
    const Vec3f corner_functor::dir[5] = {-I, J, I, -J, -I};
    Siemel Naran, Apr 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neno
    Replies:
    2
    Views:
    3,229
  2. Javier
    Replies:
    2
    Views:
    532
    James Kanze
    Sep 4, 2007
  3. Christopher
    Replies:
    12
    Views:
    739
    James Kanze
    Dec 18, 2007
  4. fungus
    Replies:
    13
    Views:
    862
    fungus
    Oct 31, 2008
  5. Luca Cerone
    Replies:
    0
    Views:
    227
    Luca Cerone
    Mar 2, 2012
Loading...

Share This Page