1

I have been looking for a reply for this a lot, but I cannot find nothing. My compiler doesn't give me any error or warning, but maybe there could be any danger into doing this:

class Dog
{
   Dog(): x(0) {}
   int x;
};

If I have a simple class, creating a function in another class like this:

class PetHouse
{
    void addDog(Dog& animal = Dog())
    {
       // Anything...
    }
};

Is the addDog() declaration right? I have an argument which is a reference and it's default value is a Dog() object, instead an existing object.

Is there any danger?

Thanks for reading!

1 Answers1

1

No, it isn't. It should not even compile, because non-const lvalue references do not bind to temporaries like Dog().

As @StoryTeller hints, you are probably using MSVC without /permissive-.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • 1
    Thanks I'm reading about /za and /permissive- flags. First time I hear about them in the 7 years I have been coding! – Juan JuezSarmiento Oct 13 '18 at 16:09
  • 1
    @JuanJuezSarmiento You are welcome! :) MSVC has never been a "proper" C++ compiler; although lately (2017 onward) is way, way better than previous releases in that regard. Hopefully they will finish their efforts in the upcoming 2019 compiler and finally provide a conformant C++ compiler. – Acorn Oct 13 '18 at 16:27