Skip to content
Advertisement

What is the cause of the difference in return value between Linux and Windows and how to fix it?

Here is the code that I tried to return an object of a class. but I got different results from CentOs (gcc) and visual studio 2013. In the cls.cpp, with gcc, it works well, I get the results such as detector.name = “t_name”, detector.stride = 5. but the values in the detector are “” and 0 under vs2013. It seems in vs2013 the cls object is deconstructed. Why I got the different returns? and how to make it works well under visual studio? Thanks a lot.

cls.h

JavaScript

cls.cpp

JavaScript

main.cpp

JavaScript

Advertisement

Answer

You have got a copy constructor that does nothing.

JavaScript

GCC probably does copy elision in the lines cls tt = func(); and cls detector = builder->build(); and it magically works. Maybe VS will do it too once you enable optimizations. Personally, I’d say it’s a bug in your code. If you have a copy constructor, then copy the origin object.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement