473,399 Members | 3,401 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,399 software developers and data experts.

is there anything like static constructors or destructors in C++ ?

is there anything like static constructors or destructors in C++
? if yes, how to implement it?

Thanks,
Amit.

Jul 23 '05 #1
3 18793
Amit wrote:
is there anything like static constructors or destructors in C++
? if yes, how to implement it?


What behavior do you think they would represent? There is probably a way to
implement that behavior, such as in-place construction.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #2
> is there anything like static constructors or destructors in C++
? if yes, how to implement it?


How do you mean? Constructors initialise and destructors uninitialise
objects, so they need an object to act upon. Thus, they cannot be static. Or
you are using some other meaning of words "constructor", "destructor" and
"static"!?

One thing comes to mind... You can have plain functions or static class
methods which can create objects ("factory methods"), this way:

class SomeClass
{
public:
SomeClass &operator=(SomeClass const &org) { /* implement assignment
operator if not trivial */ }
~SomeClass() { /* implement destructor if not trivial */ }

static SomeClass CreateSomeClass(/* for example */ int param1, bool
param2)
{
SomeClass retval(param1);
if (param2) { retval.DoSomeStuff(); }

return retval;
}

private:
explicit SomeClass(/* for example*/ int param) { /* implement a
constructor */ }
SomeClass(SomeClass const &org) { /* implement copy constructor if not
trivial */ }

void DoSomeStuff() { /* ... */ }

/* ... */
};

In this example, you can create SomeClass objects only calling
CreateSomeClass with two parameters supplied. The value is returned via the
private copy constructor, while it is created by invoking a private
constructor of any kind (in the above example - one-parameter constructor)
with any further processing needed to create the object of a desired kind
(in the above example - conditional call to DoSomeStuff()). Note that this
approach requires one or two copy constructor calls to create a SomeClass
object (depending on how good the compiler optimisations are), so it may not
be efficient for a particular purpose. Also note that it is usually not good
to have this kind of copy semantics for polymorphic classes (e.g. classes in
a inheritance hierarchy).

You could move CreateSomeClass outside of SomeClass by declaring it as a
friend of SomeClass, if desired.

Rade
Jul 23 '05 #3
* Amit:
is there anything like static constructors or destructors in C++?
Yes, semantically, but not there's no special syntax.

Dynamic initialization of static variables is performed before the first
call of a routine in the compilation unit.

Gotcha 1: initialization order _between_ compilation units is not defined
(C++ doesn't support modules, it merely enables them to some degree).

Gotcha 2: some compilers will give you trouble by incorrectly optimizing
away static initialization in e.g. library code. For libraries you should
therefore provide some explicit static initialization routine.

Gotcha 3: there are some subtle problems with circular dependencies.

if yes, how to implement it?


E.g.

namespace // anon
{
struct MyStaticData
{
int pling;
double plong;

MyStaticData(): pling(1), plong(2) {}
};

MyStaticData g;
} // namespace anon

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4

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

Similar topics

467
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
5
by: ElanKathir | last post by:
Hi All ! I want to know about the constructors & Destructors, constructors ---> New Destructors --> What ? (Which keyword to use for Destructor) Thanks & Regards, ElanKathir.S.N, ASM...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
4
by: hyd | last post by:
With C++/CLI - VS2005, is it possible to have static constructors that are automatically called when the owner assembly is loading ? Otherwise, how it is possible to call it without creating an...
4
by: Morgan Cheng | last post by:
In C#, we can initialize static member variables in static constructor. Now, when to destruct these static member variables? If these variables hold external reference(e.g. log file stream), it is...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.