Question

This simple code produces some 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 would expect an output to be: cls1() as the default constructor is deleted for cls2, but the code does not output anything, though it compiles and runs fine. I am using GCC ver. 4.8.2. Compile with:

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

$ ./a.out

The question is: how should it behave?

Thank you!

Was it helpful?

Solution

You're not actually creating an instance of cls2 in your main(); you're declaring a function that returns a cls2. This is an instance of the "most vexing parse". See, for example, http://en.wikipedia.org/wiki/Most_vexing_parse

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top