Const parameter at constructor causes stackoverflow

Posted by Luca on Stack Overflow See other posts from Stack Overflow or by Luca
Published on 2010-05-29T08:49:24Z Indexed on 2010/05/29 8:52 UTC
Read the original article Hit count: 261

Filed under:
|
|

I've found this strange behavior with VS2005 C++ compiler. Here is the situation:

I cannot publish the code, but situation is very simple.

Here is initial code: it work perfectly

class Foo {
     public:
     Foo(Bar &bar) { ... }
}

The constructor implementation stores a reference, setup some members... indeed nothing special.

If I change the code in the following way:

class Foo {
     public:
     Foo(const Bar &bar) { ... }
}

I've added a const qualifier to the only constructor routine parameter.

It compiles correctly, but the compiler outputs a warning saying that the routine Foo::Foo will cause a stackoverflow (even if the execution path doesn't construct any object Foo); effectively this happens.

So, why the code without the const parameter works perfectly, while the one with the const qualifier causes a stackoverflow? What can cause this strange behavior?

© Stack Overflow or respective owner

Related posts about c++

Related posts about constructor