开发者

sequence of conversions with contsructor-type conversions and conversion operators

I have read that a standard conversion can precede or follow a conversion implemented by a conversion operator or a contructor type conversion. On the other hand, a sequence of two conversion operators is not allowed

a sequence of two contructor type conversions is not allowed

I set out to test this and got a different result. I am using MSVC2010

In the first bunch code this fails: int b1 = sMe; which is great as it implies a sequence of two conversion operators: one from myString to myType and the other from myType to int

In the second bunch code this DOESNT fail: myString sYou(b); although I believe it implies a sequence of two constructor conversions: one from int to myType, the other from myType to myString.

Can somebody explain to me what I am missing?

Many Thanks,

FIRST BUNCH

class myType {
public:
    myType(): val(10) {}
    myType(const myType &orig): val(orig.val) {}
    myType(int v1): val(v1) {} 

    bool hasSameValue(const myType &o2) {
        return (o2.val == val); }
    int getVal() {
        return val; }
    operator int() { return val; }

private:
    int val;

};


#include <string>
class myString {
public:
    myString(): val("I Dont Know you") {}
    myString(const myString &orig): val(orig.val) {}
    myString(myType v1): val("Really Dont know you") {} 

    bool hasSameValue(const myString &o2) {
        return (o2.val == val); }
    std::string getVal() {
        return val; }
    std::string getString() {return val;}

    operator myType() { return 1000; }


private:
    std::string val;

};




#include <iostream>
using namespace std;

int main() {

    int b = 36;

    myString sMe;
    myString sYou(b);
    cout << "sYou: " << sYou.getString() << endl;
    cout << "sMe: " << sMe.getString() << endl;

    myType a = sMe;
    cout << a.getVal() << endl;
    int b1 = sMe;

    return 1;

}

SECOND BUNCH

class myType {
public:
    myType(): val(10) {}
    myType(const myType &orig): val(orig.val) {}
    myType(int v1): val(v1) {} 

    bool hasSameValue(const myType &o2) {
        return (o2.val == val); }
    int getVal() {
        return val; }

private:
    int val;

};


#include <string>
class myString {
public:
    myString(): val("I Dont Know you") {}
    myString(const myString &orig): val(orig.val) {}
    myString(myType v1): val("Really, I Dont Know you") {} 

    bool hasSameValue(const myString &o2) {
        return (o2.val == val); }
    std::string getVal() {
        return val; }
    std::string getString() {return val;}


private:
    std::string val;

};




#include <iostream>
using namespace std;

int main() {
    myType me;
    int a = 34;
    int b = 36;

    myType you(a);
    bool sameVal = you.hasSameV开发者_如何学Goalue(b);  
    cout << sameVal << endl;
    cout << "you: " << you.getVal() << endl;
    cout << "me: " << me.getVal() << endl;

    myString sMe;
    myString sYou(b);
    cout << "sYou: " << sYou.getString() << endl;
    cout << "sMe: " << sMe.getString() << endl;


    return 1;

}


myString sYou(b); only involves one implicit conversion. The second conversion is explicit; you're calling the constructor. So it compiles.

Conversely, the following will not compile:

void func(myString blah) { ... }

func(b);

as this would require two implicit conversions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜