Search code examples
c++default-constructorjuce

Implicit construction with default constructor in C++


I created a simple class to pass to the sort method of a Juce Array http://www.rawmaterialsoftware.com/api/classArray.html#ac1dca4ab2895315dd85e25eaca2fcab1

It looks like this:

class XComparison
{
public:
    static int compareElements (StraightPath first, StraightPath second)
    {
        return (int) (first.xOrigin - second.xOrigin);
    }
};

When I create an instance of this to pass to the comparator, these two work:

XComparison x;
XComparison x = XComparison();

but this one gives me a compiler error:

XComparison x();

"left of '.compareElements' must have class/struct/union" on lines 74, 101, 119 of http://juce.git.sourceforge.net/git/gitweb.cgi?p=juce/juce;a=blob;f=modules/juce_core/containers/juce_ElementComparator.h;h=f976c40c7741b3df30d10e699c282a3569a49e3c;hb=HEAD#l74

Why doesn't the implicit assignment work here?

Thanks!


Solution

  • XComparison x(); is parsed as a function declaration of x, which takes no arguments and returns an XComparison.

    http://yosefk.com/c++fqa/ctors.html#fqa-10.19