0

What is promotion constructor? does it have any relation with copy constructor/assignment operator? I have seen one example however unable to understand that.

#include <iostream>
#include <vector>

using namespace std;

class base{
    public:
    base(){
        cout << endl << "base class default construtor" << endl;
    }
    base(const base& ref1){
        cout << endl << "base class copy constructor" << endl;
    }
    base(int) { 
        cout << endl << "base class promotion constructor" << endl;
    }
};

class derived : public base{
    public:
        derived(){
            cout << endl << "derived class default constructor" << endl;
        }
        derived(const derived& ref2){
            cout << endl << "derived class copy constructor" << endl;
        }
        derived(int) {
            cout << endl << "derived class promotion constructor" << endl;
        }
};

int main(){

    vector<base> vect;
    vect.push_back(base(1));
    vect.push_back(base(1));
    vect.push_back(base(2));

    return 0;
}

When I compile and execute: than the order has come like this:

base class promotion constructor

base class copy constructor

base class promotion constructor

base class promotion constructor

base class copy constructor

base class promotion constructor

base class promotion constructor

base class copy constructor

base class promotion constructor

Please help me to understand this concept of promotion constructor. I have searched on net however didn't get much info on this.

Thanks

hims
  • 325
  • 3
  • 10
  • what else do you expect ? you never construct `derived` objects... – UmNyobe Jan 23 '14 at 10:42
  • My guess is you're not finding anything for "Promotion constructor" because that's not the right term for it. – Borgleader Jan 23 '14 at 10:43
  • @UmNyobe: I asked what is promotion constructor????? – hims Jan 23 '14 at 10:43
  • that term is uncommon. Never heard of it before. – UmNyobe Jan 23 '14 at 10:43
  • This code is from an interview. The person who wrote this created his own term "Promotion constructor" to designate something else. Likewise I can ask you `what are the differences between "creators" in this code` when I am referring to constructors. – UmNyobe Jan 23 '14 at 10:48

2 Answers2

1

First time I ever heard the term "promotion constructor". Both constructors that are named that way in your code fit the definiton of a converting constructor.

Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141
0

What is promotion constructor?

It's not a standard term in C++. Promotion refers to some of the type conversions applied automatically to built-in numeric types, and doesn't involve classes or constructors.

The constructors in your example are converting constructors: non-explicit constructors taking a single parameter, which can be used to convert that parameter type into the class type.

Being non-explicit, they can be used both for explicit conversions, like the base(1) in your example, and implicit conversions, like

vect.push_back(42);

which your example doesn't demonstrate.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644