Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
C++
class a()
{
a();
`~a();
}
class b()
{
b();
~b();
}


What will be the order for execution of Constructor & Destructor?
Posted
Updated 20-Sep-10 23:08pm
v2
Comments
Sandeep Mewara 21-Sep-10 13:51pm    
It will take 5-10min... Try out yourself! Whats the problem?
Philippe Mori 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 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

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.
 
Share this answer
 
Your code doesn't make sense: you cannot have parenthesis after a class declaration class a().
 
Share this answer
 
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
 
Share this answer
 
are you sure, you looking for this

C#
class a
{
    a();
    ~a();
};

class b
{
    b();
    ~b();
};


as I doubt you might be looking for this:->

C#
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.
 
Share this answer
 
there is just a declaration, and no executive code, like objects creation, etc. so there will be no sequence
 
Share this answer
 
Hi,
visit below for more about constructor and destructor

What is destructor[^]

What is a static constructor[^]
 
Share this answer
 
Comments
Philippe Mori 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900