Jake
Jake

Reputation: 11430

How to read this operator declaration and implementation?

The code in question:

struct PCArea {
        PCArea(
            int minxx = 0, 
            int minyy = 0, 
            int maxxx = 0, 
            int maxyy = 0
        ) {}
    };

struct NDCVolume {
    NDCVolume() {}

    operator PCArea() const;
};

// how does this operator work? how to use/read it?
NDCVolume ::operator PCArea() const {

    return PCArea(iminx, iminy, imaxx, imaxy); 
}

Redundant code has been removed from the snippet. I have used Visual Studio > Find All References but cannot spot any where it is being used. To me, it looks like a member method without a specified return value.

How is this different from below?

PCArea NDCVolume::PCArea() const;

Upvotes: 1

Views: 82

Answers (2)

Andrew
Andrew

Reputation: 24846

It's a conversion operator.

In case of NDCVolume NDCVolume::PCArea() const; it's just a function and will not be used implicit

in case of conversion operator defined you can write

NDCVolume vol;
PCArea area = vol; //implicit conversion

in the second case (with regular function) you will have to make it explicit:

NDCVolume vol;
PCArea area  = vol.PCArea(); //explicit conversion
PCArea area2 = vol; //error, if no conversion operator is defined

Upvotes: 1

ForEveR
ForEveR

Reputation: 55887

implicit conversion operator to type PCArea.

PCArea NDCVolume::PCArea() const;

is only function, not conversion operator, cannot be used automatically.

Upvotes: 0

Related Questions