473,569 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialization and destruction order for class members

Hello,

I have a class that will eventually look something like this:

class TTableHolder
{
private:
boost::scoped_p tr<TSessionFSes sion;
boost::shared_p tr<TTableFTable ;

public:
TTableHolder(TS erver &AServer)
: FSession( new TSession( &AServer ) ),
FTable( new TTable, TableReleaser( FSession.get() ) ) {}
};

I am pretty sure that the order of initialization for members in an
initializer list is based on the order of their declaration in the class.
That is, since FSession is declared before FTable, I can write the
initializers in any order I want, and FSession will always be initialized
before FTable, thus guaranteeing (I think) that FTable will get a valid
FSession object when it is initialized.

I have two questions:

1) Is my understanding of the order of member initialization correct? Or is
it compiler-dependent?

2) Assuming initialization order is specified by the language, and is based
on member declaration order, what is the order of their destruction? Are
they destructed in reverse order? Obviously, I want FTable (which is a
shared_ptr) to be destroyed before FSession, as FSession is used by FTable's
custom deleter.

Should I prefer to make FSession a shared_ptr instead? If I do that, will
the existence of FTable's custom deleter guarantee that the reference count
of FSession is not decremented to zero, thereby ensuring that FSession will
be valid at the time of FTable's destruction?

Is there a better way to write this that will guarantee the desired
construction/destruction order of class members?

Thanks,

Dennis
Jan 4 '07 #1
2 4304
Dennis Jones wrote:
I have a class that will eventually look something like this:

class TTableHolder
{
private:
boost::scoped_p tr<TSessionFSes sion;
boost::shared_p tr<TTableFTable ;

public:
TTableHolder(TS erver &AServer)
: FSession( new TSession( &AServer ) ),
FTable( new TTable, TableReleaser( FSession.get() ) ) {}
};

I am pretty sure that the order of initialization for members in an
initializer list is based on the order of their declaration in the
class.
That's correct.
That is, since FSession is declared before FTable, I can write
the initializers in any order I want, and FSession will always be
initialized before FTable, thus guaranteeing (I think) that FTable
will get a valid FSession object when it is initialized.
Right.
I have two questions:

1) Is my understanding of the order of member initialization correct?
Or is it compiler-dependent?

2) Assuming initialization order is specified by the language, and is
based on member declaration order, what is the order of their
destruction?
Reverse to their construction.
Are they destructed in reverse order? Obviously, I
want FTable (which is a shared_ptr) to be destroyed before FSession,
as FSession is used by FTable's custom deleter.
Should be alright.
Should I prefer to make FSession a shared_ptr instead? If I do that,
will the existence of FTable's custom deleter guarantee that the
reference count of FSession is not decremented to zero, thereby
ensuring that FSession will be valid at the time of FTable's
destruction?
Is there a better way to write this that will guarantee the desired
construction/destruction order of class members?
If your 'FSession' is essentially owned by FTable, then it might be
better if the ownership is actually expressed, and not implied.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 5 '07 #2

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:en******** **@news.datemas .de...
>
If your 'FSession' is essentially owned by FTable, then it might be
better if the ownership is actually expressed, and not implied.
Thanks for your reply, Victor. Looks like I am safe in trusting the order
of initialization and destruction.

No, 'FSession' is not owned by "FTable," in fact, the relationship is
actually the other way around -- a TSession owns zero or more TTables
(though my sample code does not reflect this). TSession is responsible for
destroying any TTables it owns when it is destroyed, but there are times
when a TTable must be destroyed without destroying the TSession that owns
it, in which case, the TSession must help with its destruction, which is why
I provide a custom deleter.

However, your question does help me to identify some issues that I still
need to think through as I design the system.

Thanks,

- Dennis
Jan 5 '07 #3

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

Similar topics

50
3751
by: Charles Stapleton | last post by:
Given the folowing class class Ctest{ public: Ctest( int i, int j) :a(i) { b = j; } private: int a, b; } When creating an object of type Ctest, what advantage is there to setting
3
3587
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a...
18
5420
by: Makis Papapanagiotou | last post by:
Hello all, There is a strange case where a class Test is composed from three objects of other classes. i.e. class Test { public: Test(); private: Point x;
5
2566
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
6
2154
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
11
2408
by: asdf | last post by:
The oder of member initialization is the order in which the members are defined. So the following code is problematic: class X{ int i; int j; public: X(int val):j(val),i(j){}
8
8914
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
7
3396
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case class is derived as virtual? How does the order of construction/destruction is impacted if the base class is derived as virtual or non virtual? just...
8
2432
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of declaration in the class, instead of the order they appear in the constructor initializer. This would introduce interdependencies. Therefore, it...
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7968
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.