2
#include<iostream>  
using namespace std;  
class base {  
public:     
    base() {
        cout<<"Constructing base \n";
    }  
    virtual ~base() {
        cout<<"Destructing base \n";
    } 
};  
class derived: public base { 
public:    
    derived() {
        cout<<"Constructing derived \n";
    }    
    ~derived() {
        cout<<"Destructing derived \n";
    }
};   
int main(void) {
    derived d();     
    return 0; 
} 

Why in this program its not calling constructor?

Can Anyone explain?
.......

johnchen902
  • 9,531
  • 1
  • 27
  • 69
Asap
  • 31
  • 3
  • 1
    please intent your code properly. – billz Jul 23 '13 at 10:05
  • You should call base() ctor in derived(). derived() : base() { cout << "Constructing derived"; } – bkausbk Jul 23 '13 at 10:06
  • 3
    uhm...Most vexing parse again. – Alok Save Jul 23 '13 at 10:08
  • `derived d();` does _not_ define a variable, it _declares a function_ (taking no parameter and returning a `derived`). See [Variable Initialization](http://www.gotw.ca/gotw/001.htm) and [Variable Initialization – or Is It?](http://herbsutter.com/2013/05/09/gotw-1-solution/). @bkausbk Not needed, it's done implicitly by the compiler. – gx_ Jul 23 '13 at 10:10
  • @gx_: You are right :) – bkausbk Jul 23 '13 at 10:11

6 Answers6

5

The problem is that you are declaring a function here:

// function d(), returns a derived object.
derived d();   

What you need is

derived d; // C++03, C++11

or

derived d{}; // C++11 only

This is an "interesting" aspect of C++, where anything that can be parsed as a function declaration will be (provided it is in a context where a function can be declared).

See more on variable initialization in GoTW #1 Variable initialization - or is it?.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

Try like this:

int main(void) {  
    derived d;     
    return 0; 
} 

When you type derived d() you are declaring a function and not creating object.

jtomaszk
  • 9,223
  • 2
  • 28
  • 40
3

Declaration not proper for derived d();

Change it to:

int main(void) 
{
  derived d;     // note the difference     
  return 0; 
}    

What you are doing -> derived d(), it declares a function. that returns instance of derived class.

See this for details: http://en.wikipedia.org/wiki/Most_vexing_parse

Shumail
  • 3,103
  • 4
  • 28
  • 35
2

It is because derived d() is treated as "function d, called without any arguments, returning instance of derived". Google 'most vexing parse'.

magor
  • 359
  • 1
  • 11
2
int main(void)
{  
    derived d();     //here you are actually declaring a function
    return 0; 
} 

You should do it like this:

derived d;

Then it will work.

1

In C++, before a compiler calls a function, it must be aware of the function. Either you need to write the function in the flow before it is called or it should at least be declared before the function is called. So by calling derived d() you have declared a function called d() with return type as derived object, you never created derived object. it should have been like,

int main() {
   derived d;
   return 0

}
sakthisundar
  • 3,278
  • 3
  • 16
  • 29