How should the constructor inherit?

This simple code gives unexpected results. At least for me...

#include <iostream>
class cls1
{
public:
    cls1(){std::cout << "cls1()" << std::endl;};
    cls1(int, int) : cls1() {std::cout << "cls1(int, int)" << std::endl;}
};

class cls2 : public cls1
{
public:
    using cls1::cls1;
    cls2() = delete;
};

int main()
{
    cls2 c();
    return 0;
}

I expect the result to be: cls1 (), since the default constructor is deleted for cls2, but the code does not output anything, although it compiles and works fine. I am using GCC ver. 4.8.2. Compile with:

$ g++ -std=c++11 -g test.cpp

$ ./a.out

Question: how should he behave?

Thanks!

+4
source share
1 answer

In fact, you are not creating an instance cls2in main(); you declare a function that returns cls2. This is an example of "the most unpleasant parsing." See, for example, http://en.wikipedia.org/wiki/Most_vexing_parse

+2
source

Source: https://habr.com/ru/post/1527001/


All Articles