lundi 3 août 2015

Can delegation constructor in C++ be called in the body or just in the initializer list?

as the title says I am curious if delegation constructor can be called in the body or not.

If you are curious about motivation: I have some condition and only workaround I found requires having uselss write on dummy member that occupies space.

#include <iostream>
using namespace std;

struct Delegating {
Delegating (const string& str) {
        cout <<"const string& \n";
}
Delegating(string&& str): dummy_(str.size()>4 ? 0 : (Delegating(str),0))     {
       if (str.size()>4) {
          cout <<"string&& \n";
       }
}
bool dummy_;
};

int main() {
    // your code goes here
    Delegating("abc");
    Delegating("abcde");
    Delegating("abcde");
    Delegating("abcde");
    Delegating("abc");
    cout << "\n--------------------\n";
    Delegating(string("abc"));
    Delegating(string("abcde"));
    Delegating(string("abcde"));
    Delegating(string("abcde"));
    Delegating(string("abcde"));
    Delegating(string("abc"));
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire