How to initialize a static array of a class in a static member function
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to initialize a static array of a class in a static member function
Hi
What i want to do is initialize a static array that i have in class and initialize it using a static memmber function of that class.
Here's what i want:[HTML]
// This is .h file
class Test
{
private:
const static int size=10;
static int a[size];// Array Declaration
static void initArray();
public:
Test();
}
// This is .cpp file
#include"Test.h"
int Test::a[size];// Array Definition
Test::Test()
{
initArray();// To initialize the array members
}
void Test::initArray()
{
for(int i=0;i<size;i++)
{
a[i]=i;
}
}
[/HTML]
However the above code doesnt compile and i want to know WHY
I know that if i want to initialize the array i can do the following in .cpp file
int Test::a[size]={1,2,3,4,5,6,7,8,9,10};
However this does not solve my purpose. As if i change the value of size later i would have to make a change in this initialization as well.
Moreover i want to know why the hell C++ doen't allow this to happen
Hi I Am really Sorry It works even for me but What i want is that instead of int i want pthread_mutex_t i.e
THE FOLLOWING IS THE CODE HAVING PROBLEMS I AM SORRY FOR THE ABOVE CORRECT CODE
// .h file
[HTML]
#include<pthread.h>
class Test
{
private:
static const int size=10;
static pthread_mutex_t a[size] //i.e i want an array of mutex locks
static void initArray();
public:
Test();
};
// This is the .cpp file i.e implementation file
#include"Test.h"
pthread_mutex_t Test::a[size]//This is the definition of the array;
void Test::initArray()
{
for(int i=0;i<size;i++)
{
a[i]=PTHREAD_MUTEX_INITIALIZER;// If i comment this line code works fine and this is the line compiler says as error
}
}
Test::Test()
{
initArray();
}
[/HTML]
Now this code really gives me stupid errors. Very simple errors.
I know that there is something wrong with my usage of pthread_mutex_t or PTHREAD_MUTEX_INITIALIZER.
However the code works fine if i do the initialization as ;
pthread_mutex_t Test::a[size]={PTHREAD_MUTEX_INITIALIZE,...10 times}
However i dont want this and want that my array of mutex locks shd be initialized in my static function initArray so that when i change size i dont have to edit the initialization again.
Please guide me i have been trying this for past 3 hours
Also i dont know much about threaded programming and i guess thats what is causing the problem.
Hy,
I also had problems using the PTHREAD_MUTEX_INITIALIZER
inside the code, so I always use the pthread_mutex_init().
In your case you just need to write:
This is a great site man. Never ben disappointed. NEVER !!!!
Anyway, i read on net about using PTHREAD_MUTEX_INITIALIZER when the lock has been declared as static.
And also i overheard some 1 about using only PTHREAD_MUTEX_INITIALIZER.
May be i am wrong.
So some 1 please provide me with details and any nice material on pthreads and how to use them in C++ would be appreciated i.e how to use the pthread library i. i want some book or any online material. I have googled but want your suggestions as well.
PTHREAD_MUTEX_INITIALIZER is a macro for something like {{0,0,0,0,0{0}}} which is a C-struct initializer and can't be used except at the point of definition.
It's very bad to have the constructor of every instance call the init function without some sort of control. That function either needs a static flag that says when it's been inited, or (see next):
So some 1 please provide me with details and any nice material on pthreads and how to use them in C++ would be appreciated i.e how to use the pthread library i. i want some book or any online material. I have googled but want your suggestions as well.
If you look at info libc there should be a pthread reference in there. It shows up very nicely in Konqueror under info:libc.
The main thing to remember is that it's C++-style to use as little static data as possible, but most Unix C requires a lot of static data, as well as initializer functions that need to be explicitly called somehow by main. This is the only safe way to make sure everything is initialized properly, especially when you have 10+ source files.
You always need to keep in mind that threads are global, and therefore exist outside of all other threads. This means that as functions (and even if/for/while blocks) begin and end and their stack objects come and go, outside threads aren't aware of that. That's why most of the outside data actually accessed by a thread needs to be static, so that other threads' stack states don't cause the thread in question to access data that's no longer there.
ta0kira
It's very bad to have the constructor of every instance call the init function without some sort of control. That function either needs a static flag that says when it's been inited, or (see next):
Code:
if (!init_done)
{
for(int i=0;i<size;i++)
pthread_mutex_init(&a[i], NULL);
init_done = true;
}
}
Thanks ta0kira. Actually it was my mistake. What i am doing is that this CTOR is being called from getinstance static function of a singleton class. So this ctor is going to be called only once.
Thanks for for indepth insight.
Moreover, i would like some suggestiosn from you.
What i have been always curious is to see how STL is being implemented behind the scenes.
However i have googled a lot but not seen a single implementation i.e behind the scenes source code to say implement map or set or say vector.
And if STL just specifies the interface and every1 can just implement their own implementation don't you think that it would be inefficient.
Like for some compilers STL provided would be efficient and for some it won't be efficient
I may be totally wrong in whatever i have mentioned above. So please provide details etc.
Also i have seen sgi site but even there implementation is not there.
Who is the one who makes sure that STL is efficient.
I just love STL. And in future even i want to have STL like generic classes. So was a bit curious.
Thanks ta0kira. Actually it was my mistake. What i am doing is that this CTOR is being called from getinstance static function of a singleton class. So this ctor is going to be called only once.
Why not make the array non-static then? That will also allow you to use it in another source for the same project later on if you want to.
Quote:
Originally Posted by lali.p
And if STL just specifies the interface and every1 can just implement their own implementation don't you think that it would be inefficient.
Templates aren't actually classes/functions. They are literally templates for creating real classes and functions. When you declare or define an object using a template then that template is "instantiated." Nothing really happens then except for creation of code associated with the constructor, destructor, and template functions they might use. Each time you use a template function or call a function on a template-derived object, that function is instantiated, and therefore code is created for it. Unused functions literally aren't compiled if they aren't used (but they can cause parsing errors.) Definitions of virtual functions should be considered "used" because the vtable of the class will need them.
In order to be able to instantiate a template, its code must be out in the open. That means that the source code for the STL is in the STL headers you actually include. Unlike C headers that have libc behind them, all of the templates in the STL define their functions in the same place they declare them. Take a look at /usr/include/c++/*. Most of those files will include something from a subdirectory which is where most of the code is defined.
Templates are normally defined only in headers with no source files. This is because templates are generally inlined, so you don't have to worry about multiple definition linking errors. Also, some sources might use functions that others don't and therefore will need to force instantiation.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.