Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
class a()
{
a();
`~a();
}
class b()
{
b();
~b();
}
 
What will be the order for execution of Constructor & Destructor?
Posted 21-Sep-10 0:06am
Edited 21-Sep-10 0:08am
Johnny J.117.4K
v2
Comments
Sandeep Mewara at 21-Sep-10 13:51pm
   
It will take 5-10min... Try out yourself! Whats the problem?
Philippe Mori at 12-Jan-13 9:12am
   
The question would not be as bad if the code would compile and be complete... As written the question does not make much sense as there is no cxode to execute.
kaushik4study at 25-Jan-13 15:57pm
   
think again..and put here right question..may you did it .in hurry please modify so some one can help
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The contructor is called when an object of the class is instantiated (created) and the destructor is called when the object is destroyed. It is not a question of how you code it in your source but of following the rules for the life-cycle class objects.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Your code doesn't make sense: you cannot have parenthesis after a class declaration class a().
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Well your code won't compile so the question is a bit moot. To get your code to compile you'd have to rewrite it along the lines of the following:
 
class a
{
    a();
    ~a();
};
 
class b
{
    b();
    ~b();
};
 
Unfortunately it won't actually do anything at this point - all you've done is describe a pair of classes to the compiler and not actually told it to do anything.
 
However as soon as you actually try and create objects of type a or b the compiler will complain that the constructors for the objects are private and it can't create objects of those types.
 
When you fix that you'll probably find the linker complaining that it can't find definitions of a::a(), a::~a(), b::b() and b::~b(). By the time you fix that lot you'll have a program that you can probably execute AND find out the answers to what you want to know.
 
Cheers,
 
Ash
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

are you sure, you looking for this
 
class a
{
    a();
    ~a();
};
 
class b
{
    b();
    ~b();
};
 
as I doubt you might be looking for this:->
 
class a
{
    a();
    virtual ~a();
};
 
class b: public a
{
    b();
 virtual ~b();
};
 
In above case, A constructor will be called, then B constructor will be called.
 
and at the time of destroying the B destructor will be called then A.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

there is just a declaration, and no executive code, like objects creation, etc. so there will be no sequence
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Hi,
visit below for more about constructor and destructor
 
What is destructor[^]
 
What is a static constructor[^]
  Permalink  
Comments
Philippe Mori at 12-Jan-13 9:09am
   
It is a nice idea to suggest the user to search... but given links are not very good. First the explanation is rudimentary and second, static constructor are not regular constructor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100