473,388 Members | 1,335 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,388 software developers and data experts.

initialization and destruction order for class members

Hello,

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

class TTableHolder
{
private:
boost::scoped_ptr<TSessionFSession;
boost::shared_ptr<TTableFTable;

public:
TTableHolder(TServer &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 4294
Dennis Jones wrote:
I have a class that will eventually look something like this:

class TTableHolder
{
private:
boost::scoped_ptr<TSessionFSession;
boost::shared_ptr<TTableFTable;

public:
TTableHolder(TServer &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.********@comAcast.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
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
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...
18
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
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
6
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
11
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
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....
7
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...
8
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.