Discussion:
[Dev-C++] static vector initialization
Anderson Ilha dos Santos
2002-10-14 18:19:05 UTC
Permalink
Greetings,

I have been working on a set of utility functions that I gruped together on an
entirely static class. The
private part of this class has a (static) STL vector container whose components
I have had trouble to
intialize. My class partial listing can be given as below,

// file utility.hpp
class Utility
{
// public interface ...

private:
static const int size = 10;
static vector<double> vec;
}

// file utility.cpp
#include "Utility.hpp"
const int Utility::size;
vector<double> Utility::vec( size );

the private member 'size' can be directly initialized in the class body because
it is a static
integral value. However, I was unable to intialize the vec components (vec[0],
vec[1], etc.) directly
below the vector<double> vec declaration in the utility.cpp file, just as I
would have for the other
static private members of the class. The only way I have found to initiliaze
the vec components was
to use a new function, say 'void init_vec()', and then initialize the
components inside this new function.

I would like to know whether this is indeed the case or if I am missing
something. The init_vec()
approach seems to be something like a "makeshift" constructor, and I would
prefer to be able to
initialize the container components just as I would have to a usual static
member.

I will appreciate any advice you guys can give me.

Regards, Anderson.
Ioannis Vranos
2002-10-14 21:37:03 UTC
Permalink
-----Original Message-----
Of Anderson Ilha dos Santos
Sent: Monday, October 14, 2002 11:11 PM
Subject: [Dev-C++] static vector initialization
Greetings,
I have been working on a set of utility functions that I
gruped together on an
entirely static class. The
private part of this class has a (static) STL vector
container whose components
I have had trouble to
intialize. My class partial listing can be given as below,
// file utility.hpp
class Utility
{
// public interface ...
static const int size = 10;
static vector<double> vec;
}
// file utility.cpp
#include "Utility.hpp"
const int Utility::size;
Not needed. You could do const int Utility::size=10; inside the .hpp
file but you do it in the shorter way anyway (which is equivallent).
vector<double> Utility::vec( size );
the private member 'size' can be directly initialized in the
class body because
it is a static
integral value. However, I was unable to intialize the vec
components (vec[0],
vec[1], etc.) directly
below the vector<double> vec declaration in the utility.cpp
file, just as I
would have for the other
static private members of the class. The only way I have
found to initiliaze
the vec components was
to use a new function, say 'void init_vec()', and then initialize the
components inside this new function.
I would like to know whether this is indeed the case or if I
am missing
something. The init_vec()
approach seems to be something like a "makeshift"
constructor, and I would
prefer to be able to
initialize the container components just as I would have to a
usual static
member.
I will appreciate any advice you guys can give me.
At first i do not think you need a private static vector but anyway. If
you have another vector you can copy it directly:

vector<double> Utility::vec( othervector );

If you have another container e.g. a built in array you can do:

double darray[]={1,2,3,4,5};
// ...

vector<double> Utility::vec(darray, darray+5*sizeof(*darray));

// sizeof(*darray)==sizeof(darray[0])==sizeof(double);

You can do the same for every other container like list etc.


If you do not want to copy, you have to assign every element explicitly
as you did.


Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
Daniel Glenfield
2002-10-16 09:07:02 UTC
Permalink
I am sure that the only way to initialise static members si with a static
member function, but I don't know about vectors.

Is static const int size = 10; actually allowed?
Subject: [Dev-C++] static vector initialization
Date: Mon, 14 Oct 2002 18:10:36 -0200
Greetings,
I have been working on a set of utility functions that I gruped together on
an
entirely static class. The
private part of this class has a (static) STL vector container whose
components
I have had trouble to
intialize. My class partial listing can be given as below,
// file utility.hpp
class Utility
{
// public interface ...
static const int size = 10;
static vector<double> vec;
}
// file utility.cpp
#include "Utility.hpp"
const int Utility::size;
vector<double> Utility::vec( size );
the private member 'size' can be directly initialized in the class body
because
it is a static
integral value. However, I was unable to intialize the vec components
(vec[0],
vec[1], etc.) directly
below the vector<double> vec declaration in the utility.cpp file, just as I
would have for the other
static private members of the class. The only way I have found to
initiliaze
the vec components was
to use a new function, say 'void init_vec()', and then initialize the
components inside this new function.
I would like to know whether this is indeed the case or if I am missing
something. The init_vec()
approach seems to be something like a "makeshift" constructor, and I would
prefer to be able to
initialize the container components just as I would have to a usual static
member.
I will appreciate any advice you guys can give me.
Regards, Anderson.
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
Broadband? Dial-up? Get reliable MSN Internet Access.
http://resourcecenter.msn.com/access/plans/default.asp
Per Westermark
2002-10-16 09:19:02 UTC
Permalink
Depends on compiler version. If you need backwards compatibility, it
shouldn't be used.

/Per W
I am sure that the only way to initialise static members si with a static=
=20
member function, but I don't know about vectors.
=20
Is static const int size =3D 10; actually allowed?
=20
=20
=20
=20
=20
=20
Subject: [Dev-C++] static vector initialization
Date: Mon, 14 Oct 2002 18:10:36 -0200
Greetings,
I have been working on a set of utility functions that I gruped together=
on=20
an
entirely static class. The
private part of this class has a (static) STL vector container whose=20
components
I have had trouble to
intialize. My class partial listing can be given as below,
// file utility.hpp
class Utility
{
// public interface ...
static const int size =3D 10;
static vector<double> vec;
}
// file utility.cpp
#include "Utility.hpp"
const int Utility::size;
vector<double> Utility::vec( size );
the private member 'size' can be directly initialized in the class body=
=20
because
it is a static
integral value. However, I was unable to intialize the vec components=20
(vec[0],
vec[1], etc.) directly
below the vector<double> vec declaration in the utility.cpp file, just a=
s I
would have for the other
static private members of the class. The only way I have found to=20
initiliaze
the vec components was
to use a new function, say 'void init_vec()', and then initialize the
components inside this new function.
I would like to know whether this is indeed the case or if I am missing
something. The init_vec()
approach seems to be something like a "makeshift" constructor, and I wou=
ld
prefer to be able to
initialize the container components just as I would have to a usual stat=
ic
member.
I will appreciate any advice you guys can give me.
Regards, Anderson.
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
=20
=20
_________________________________________________________________
Broadband?=A0Dial-up? Get reliable MSN Internet Access.=20
http://resourcecenter.msn.com/access/plans/default.asp
=20
=20
=20
-------------------------------------------------------
This sf.net email is sponsored by: viaVerio will pay you up to
$1,000 for every account that you consolidate with us.
http://ad.doubleclick.net/clk;4749864;7604308;v?
http://www.viaverio.com/consolidator/osdn.cfm
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
=20
Ioannis Vranos
2002-10-16 14:34:03 UTC
Permalink
-----Original Message-----
Of Daniel Glenfield
Sent: Wednesday, October 16, 2002 2:06 PM
Subject: Re: [Dev-C++] static vector initialization
I am sure that the only way to initialise static members si
with a static
member function, but I don't know about vectors.
Is static const int size = 10; actually allowed?
A static member is a regular global namespace entity (object or
function) which for notational purposes is defined inside the class
definition (and the function as a member function style while it is
not).

For static integral members the initialization can be done inside the
class declaration without needing an external definition as with other
types. This is only supported by recent compiler releases (compiler
vendors had more important ANSI C++ things to implement than that in the
past).



Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys

Loading...