0

Why does the code below compile?

class Test
{
    public:
        Test(int i) {}
    private:
        Test();
};

int main()
{
    // OK - uses Test(int i)
    Test test(5);

    // Error - Test() is private
    // Test test2;

   // Why does this compile? Test() is private!
   Test test3();
}

I would think that the last instanciation would fail to compile since the no-param constructor is private?

Q-bertsuit
  • 3,223
  • 6
  • 30
  • 55

1 Answers1

2

Test test3(); is a function declaration. It declares a function named test3 of type int(). It's OK to declare things in C++, even if they're never defined (since you're not trying to actually call it).

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084