473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Alternate way to initialise static object.

Yu
I have found that the static object is initialised at the time when
the shared libary is loaded. The initialisation caused the invocation
of the constructor.
May I know of any way that I can initialize the static object without
invoking the constructor?

Below is the sample coding.

Header file ASURegistrationManager.h
#include "ASURegistration.h"

class ASURegistrationManager
{
public:
static ASURegistration& GetASURegistration();

private:
static ASURegistration ASUReg; //The static object
ASURegistrationManager();
~ASURegistrationManager();
};

Header file ASURegistration.h
#include "ApplicationInterface.h"

class ASURegistration : public IApplicationInterface
{
public:
ASURegistration();
~ASURegistration();
};

Header file ApplicationInterface.h
class IApplicationInterface
{
public:
IApplicationInterface(); //This will spawn thread
~IApplicationInterface();

};

ASURegistration.cpp
#include "ASURegistration.h"

ASURegistration::ASURegistration() : IApplicationInterface()
{
}

ASURegistration::~ASURegistration()
{
}

ASURegistrationManager.cpp
#include "ASURegistrationManager.h"

ASURegistration ASURegistrationManager::ASUReg; //This initialization
invoke the IApplicationInterface constructor to create thread.

ASURegistrationManager::ASURegistrationManager()
{
}

ASURegistrationManager::~ASURegistrationManager()
{
}

ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)ASUReg;
}

ApplicationInterface.cpp
#include "ApplicationInterface.h"

using namespace std;

void *APIProcessEvent(void *pvASUInterface)// Thread Function
{
while(true)
{
sleep(5);
}
return(0);
}

IApplicationInterface::IApplicationInterface()
{
pthread_t ProcessEventThread;
pthread_attr_t attr;

cout << "In IApplicationInterface\n";

if(pthread_attr_init(&attr) != 0)
{
std::cout<<"pthread_attr_init system call for thread
failed.\n";
}

if(pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTE M) != 0)
{
std::cout<<"pthread_attr_setscope system call for thread
failed.\n";
}

if(pthread_create(&ProcessEventThread,&attr,APIPro cessEvent,NULL)
!= 0)
{
std::cout<<"Failed to create Process Event Thread.\n";
}
}

IApplicationInterface::~IApplicationInterface()
{
}
I have the cpp function that calls the static method
ASURegistrationManager::GetASURegistration().

#include <stdio.h>

#include "wrap.h"
#include "AClass.hpp"
#include "CppLibAPI.h" //This is just a combination header file of the
header files shown above.

using namespace std;
extern "C" {

void * AClass_new(int x)
{
ASURegistration& ASUReg =
ASURegistrationManager::GetASURegistration();
return ((void *)new AClass(x)); //AClass is a cpp class
}

}

The above cpp programs are compiled as a shared library .so to be used
by the C program.

The C program is as follow.

#include <stdio.h>
#include "wrap.h"
int main(void)
{
printf("main() starting\n");
return 0;
}

When I run the C program, the output is as follow:
In IApplicationInterface
main() starting

As you can see the IApplicationInterface constructor is called which
will spawn a thread before the C main function. The static method in
the AClass_new() cpp function caused the initialisation of the static
variable ASUReg which in turns invoke the IApplicationInterface
constructor. I need the thread to be spawned only at the point when
the cpp function AClass_new() is called instead of during program
initialisation. Is there other anyway to initialise the static object
such that the constructor is not invoked?

I have tried to use a static pointer. That will not cause the
invocation of the constructor at initialisation, because a pointer can
be initialised to null. But I have concerned on the static method's
return.

If I change the declaration to
static ASURegistration* ASUReg

so ASUReg can be initialised as
ASURegistration* ASURegistrationManager::ASUReg=NULL;

Is the following statement correct?

ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)*ASUReg; //Will this cause any problem?
}

Thank you!
Jul 19 '05 #1
12 5436
Sorry about the previous post, hit the send button to early.

[snip]

If I change the declaration to
static ASURegistration* ASUReg

so ASUReg can be initialised as
ASURegistration* ASURegistrationManager::ASUReg=NULL;

Is the following statement correct?

ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)*ASUReg; //Will this cause any problem?
}

Thank you!


The above looks OK. You should remove the cast.

ASURegistration& ASURegistrationManager::GetASURegistration()
{
return *ASUReg;
}

john
Jul 19 '05 #2
Hi,

"Yu" <xi*****@yahoo.com> wrote in message
news:47**************************@posting.google.c om...
I have found that the static object is initialised at the time when
the shared libary is loaded. The initialisation caused the invocation
of the constructor.
May I know of any way that I can initialize the static object without
invoking the constructor?

Below is the sample coding.

Header file ASURegistrationManager.h
#include "ASURegistration.h"

class ASURegistrationManager
{
public:
static ASURegistration& GetASURegistration();

private:
static ASURegistration ASUReg; //The static object
ASURegistrationManager();
~ASURegistrationManager();
};
What about the following, I think it will only invoke the constructor on the
first call to GetASURegistration()

class ASURegistrationManager
{
public:
static ASURegistration& GetASURegistration() {
static ASURegistration ASUReg;
return ASUReg;
}

private:
ASURegistrationManager();
~ASURegistrationManager();
};

Thank you!

Jul 19 '05 #3
Yu
> > If I change the declaration to
static ASURegistration* ASUReg

so ASUReg can be initialised as
ASURegistration* ASURegistrationManager::ASUReg=NULL;

Is the following statement correct?

ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)*ASUReg; //Will this cause any problem?
}

Thank you!


The above looks OK. You should remove the cast.

ASURegistration& ASURegistrationManager::GetASURegistration()
{
return *ASUReg;
}

john


Hi John,
The above will crash. The following should should my problem. Thanks!

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
{
ASURegistration asuReg = ASURegistration();
ASUReg = &asuReg;
}
return *ASUReg;
}

Yu :)
Jul 19 '05 #4
Yu
Oops! Mistake, should have static as follow.

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
{
static ASURegistration asuReg = ASURegistration();
ASUReg = &asuReg;
}
return *ASUReg;
}
Jul 19 '05 #5

"Yu" <xi*****@yahoo.com> wrote in message
news:47**************************@posting.google.c om...
Oops! Mistake, should have static as follow.

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
{
static ASURegistration asuReg = ASURegistration();
ASUReg = &asuReg;
}
return *ASUReg;
}


I don't think that's valid code even. You're declaring a variable, and
assigning it the return value of a constructor? Then, getting the address
of that variable and returning its contents? I think what you want is this:

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
ASUReg= new ASURegistration();

return *ASUReg;
}

(But I would personally prefer to return the pointer itself, not a reference
to it. I didn't look at your other code to analyze how this will be called,
though.)

-Howard

Jul 19 '05 #6
"Howard" <al*****@hotmail.com> wrote...

"Yu" <xi*****@yahoo.com> wrote in message
news:47**************************@posting.google.c om...
Oops! Mistake, should have static as follow.

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
{
static ASURegistration asuReg = ASURegistration();
ASUReg = &asuReg;
}
return *ASUReg;
}
I don't think that's valid code even.


Why?
You're declaring a variable, and
assigning it the return value of a constructor?
There is no "return value of a constructor". The syntax

T var = T();

is a valid C++ for copy-initialising 'var' with a default-initialised
temporary. Whether 'var' is static or not doesn't matter. Of course,
it could be simply written as

static ASURegitration asuReg;

which basically does the same thing. The difference might be if the
type 'ASURegistration' is a typedef for a POD.
Then, getting the address
of that variable and returning its contents?
Sure, why the hell not? A variable is an lvalue, one may take
its address if one wants to. And returning a reference is not
the same as returning the contents, although that shouldn't hurt
either.
I think what you want is this:

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
ASUReg= new ASURegistration();

return *ASUReg;
}

(But I would personally prefer to return the pointer itself, not a reference to it. I didn't look at your other code to analyze how this will be called, though.)


What if there is a requirement to return a reference?

Victor
Jul 19 '05 #7

"Yu" <xi*****@yahoo.com> wrote in message
news:47**************************@posting.google.c om...
Oops! Mistake, should have static as follow.

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
{
static ASURegistration asuReg = ASURegistration();
ASUReg = &asuReg;
}
return *ASUReg;
}


This is a bit simpler (as Victor said)

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
{
static ASURegistration asuReg;
ASUReg = &asuReg;
}
return *ASUReg;
}

This is even simpler (no ASUReg pointer)

ASURegistration& ASURegistrationManager::GetASURegistration()
{
static ASURegistration asuReg;
return asuReg;
}

The rules of C++ say that asuReg will not be constructed until the first
time you call GetASURegistration.

But all of these are correct, can't tell why you get a crash.

john
Jul 19 '05 #8

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vg************@corp.supernews.com...
"Howard" <al*****@hotmail.com> wrote...

"Yu" <xi*****@yahoo.com> wrote in message
news:47**************************@posting.google.c om...
Oops! Mistake, should have static as follow.

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
{
static ASURegistration asuReg = ASURegistration();
ASUReg = &asuReg;
}
return *ASUReg;
}
I don't think that's valid code even.


Why?
You're declaring a variable, and
assigning it the return value of a constructor?


There is no "return value of a constructor". The syntax


I know there is no return value of a constructor. That's exactly why I
thought the code was wrong! I can't see why anyone would write a statement
like that when, as you point out below, it's simply not needed. I just
assumed it was illegal (falsely, apparently, which is why I said I didn't
"think" it was legal).
T var = T();

is a valid C++ for copy-initialising 'var' with a default-initialised
temporary. Whether 'var' is static or not doesn't matter. Of course,
it could be simply written as

static ASURegitration asuReg;

which basically does the same thing. The difference might be if the
type 'ASURegistration' is a typedef for a POD.
Then, getting the address
of that variable and returning its contents?


Sure, why the hell not? A variable is an lvalue, one may take
its address if one wants to. And returning a reference is not
the same as returning the contents, although that shouldn't hurt
either.


Well, that's a local variable, is why not, and will got out of scope.
BUT....am I correct in assuming that's why he added the "static" to the
declaration? In that case, the fact that it's local is not relevant...it
won't go out of scope, so it's address will stil be legal, right? So my
objection would be misplaced in that case.

Regarding my statement "returning its contents", I was referring to the line
return *asuReg. Meaning that he was dereferencing the pointer. I guess
that you're correct there that what is being returned is a reference, so
it's not really returning the "contents" of anything. I'm not used to
returning references like that, except in some operators (return *this). It
looked very strange to me, still...creating a local variable,
copy-initializing it from a temporary, and returning its address. It's just
not the usual way of doing that, I think you'll agree?
I think what you want is this:

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
ASUReg= new ASURegistration();

return *ASUReg;
}

(But I would personally prefer to return the pointer itself, not a

reference
to it. I didn't look at your other code to analyze how this will be

called,
though.)


What if there is a requirement to return a reference?


Sure, and like I said, I didn't look to see what his exact requirements
were. And I stated it was just my preference. But my method showed the
much more common way of creating an object in this manner. (BTW, Is that
what's called a singleton?)

-Howard

Jul 19 '05 #9
"Howard" <al*****@hotmail.com> wrote...

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vg************@corp.supernews.com...
"Howard" <al*****@hotmail.com> wrote...

"Yu" <xi*****@yahoo.com> wrote in message
news:47**************************@posting.google.c om...
> Oops! Mistake, should have static as follow.
>
> ASURegistration& ASURegistrationManager::GetASURegistration()
> {
> if (!ASUReg)
> {
> static ASURegistration asuReg = ASURegistration();
> ASUReg = &asuReg;
> }
> return *ASUReg;
> }

I don't think that's valid code even.
Why?
You're declaring a variable, and
assigning it the return value of a constructor?


There is no "return value of a constructor". The syntax


I know there is no return value of a constructor. That's exactly why I
thought the code was wrong! I can't see why anyone would write a

statement like that when, as you point out below, it's simply not needed. I just
assumed it was illegal (falsely, apparently, which is why I said I didn't
"think" it was legal).
T var = T();

is a valid C++ for copy-initialising 'var' with a default-initialised
temporary. Whether 'var' is static or not doesn't matter. Of course,
it could be simply written as

static ASURegitration asuReg;

which basically does the same thing. The difference might be if the
type 'ASURegistration' is a typedef for a POD.
Then, getting the address
of that variable and returning its contents?
Sure, why the hell not? A variable is an lvalue, one may take
its address if one wants to. And returning a reference is not
the same as returning the contents, although that shouldn't hurt
either.


Well, that's a local variable, is why not, and will got out of scope.
BUT....am I correct in assuming that's why he added the "static" to the
declaration? In that case, the fact that it's local is not relevant...it
won't go out of scope, so it's address will stil be legal, right?


Local objects declared static have lifetime until the end of
the program, not until the end of the block.
Regarding my statement "returning its contents", I was referring to the line return *asuReg. Meaning that he was dereferencing the pointer. I guess
that you're correct there that what is being returned is a reference, so
it's not really returning the "contents" of anything. I'm not used to
returning references like that, except in some operators (return *this). It looked very strange to me, still...creating a local variable,
copy-initializing it from a temporary, and returning its address. It's just not the usual way of doing that, I think you'll agree?
No, I don't agree. That's how most of singletons are implemented.
The static object could be a class-wide static or a local static
of some static member or some global function that returns a pointer
or a reference to it.
[...](BTW, Is that
what's called a singleton?)


Yes, it looks pretty much like a singleton implementation.
Jul 19 '05 #10

"Yu" <xi*****@yahoo.com> wrote in message
news:47**************************@posting.google.c om...
Hi John,
Crash is due to the following code.

ASURegistration* ASURegistrationManager::ASUReg = NULL;
ASURegistration& ASURegistrationManager::GetASURegistration()
{
return *ASUReg;
}

ASUReg is initialised to NULL. I need to do some kind of assignment
first before return.

There is no problem with what you and the rest suggested. Thanks a lot
to you and all the rest for your opinions.

I think what Howard suggested is fine too. But I need to take care of
the destruction of ASUReg somewhere with "delete". Am I right?

ASURegistration& ASURegistrationManager::GetASURegistration()
{
if (!ASUReg)
ASUReg= new ASURegistration();

return *ASUReg;
}


Strictly speaking you do, although I would be tempted to just leave it. If
you do a delete then obviously you have to ensure that the delete only
happens after you no longer need the ASURegistration object and in general
that can be quite hard to do.

One of the advantages of the other methods that don't use delete is that
because destructors are called in reverse order of constructors the
ASURegstration object shuld be destroyed only after all objects that used it
are destroyed.

john

Jul 19 '05 #11
>
One of the advantages of the other methods that don't use delete is that


'don't use new' is what I meant.

john
Jul 19 '05 #12
A simple code as u r trying to write a single ton class is here
//Header
class A
{
Public:
static A* p_ASingleInst;
static A* Get_SingleInstance()
{
if(!p_ASingleInst)
p_ASingleInst = new A();
return p_ASingleInst;
}
Private:
A()
}
//cpp
static A::p_ASingleInst = NULL;

I think this is what you wnated, Your const will not get called when u do
initilisation of the pointer to NULL;
Thanks
Vijay
Jul 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stephen Horne | last post by:
It just occurred to me that if decorators really catch on, there could be cases where they are applied repeatedly to many functions, and possibly with many decorators. Combine that with many (and...
13
by: Toby A Inkster | last post by:
www.authoring.stylesheets] For ages I have provided links to alternate stylesheets from my pages as per W3C recommendations: <link rel="stylesheet" href="baz" type="text/css" title="Baz"...
3
by: Annie | last post by:
hello all, is it possible at all? i have a class that encapsulates a Hashtable collection. What i want is to initialise this object once and only once and then be able to access its Public...
1
by: Eric Lindsay | last post by:
I am trying to understand the differences between and uses of persistent, default and alternate styles. I have read http://www.w3.org/TR/REC-html40/present/styles.html section 14.3.2 on...
2
by: Tomás | last post by:
Up until lately I've been writing all mad kinds of code for accomplishing things, but lately I've decided to lean more toward the whole readability, etc. side of things. I have a command line...
1
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
14
by: Frederick Gotham | last post by:
How do we initialise an aggregate member object of a class? The following won't compile for me: struct MyStruct { int i; double d; }; class MyClass { private:
5
by: Michael R | last post by:
Searching the net I've found a simple technique to add row numbers and alternate colors (for the even and the uneven row) to a continuous form. 1st step: Create a textbox, send it to background...
5
by: Owen Ransen | last post by:
When I installed the new VC 2005 I though, aha at last I will be able initialise static const members in the H file, and I can, but only integer types. double types have to be declared in the H...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.