Would u explain this code about template and class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lippman.Gan@gmail.com

    #1

    Would u explain this code about template and class

    Sorry for that I can not clarify the type of issue in topic.

    Look the code below:

    template <class T>
    class A
    {
    public:
    class a
    {
    public:
    a(int elem):x(elem){c out<<"a::a()\n" ;}
    ~a(){cout<<"a:: ~a()\n";}
    int GetX(){return x;};
    private:
    int x;
    };
    A(a _a1):a1(_a1){co ut<<"haha"<<end l;}
    private:
    a a1;
    friend bool operator ==(const A<int>& t1,const A<int>& t2)
    {
    cout<<"operator =="<<endl;
    return true;
    }

    };
    int main()
    {
    A<intb(10); //This would be OK, why?
    if(b==10){cout< <"success\n"; } //Why this can not run?
    return 0;
    }

    I see in main()
    L1, 10 is an integer it implicitly transfer to class a
    L2, 10 is also an integer, but it can not implicitly transfer.
    why L1 can do this but L2 can not?

  • Alf P. Steinbach

    #2
    Re: Would u explain this code about template and class

    * Lippman.Gan@gma il.com:
    Sorry for that I can not clarify the type of issue in topic.
    >
    Look the code below:
    >
    template <class T>
    class A
    {
    public:
    class a
    {
    public:
    a(int elem):x(elem){c out<<"a::a()\n" ;}
    ~a(){cout<<"a:: ~a()\n";}
    int GetX(){return x;};
    private:
    int x;
    };
    A(a _a1):a1(_a1){co ut<<"haha"<<end l;}
    private:
    a a1;
    friend bool operator ==(const A<int>& t1,const A<int>& t2)
    {
    cout<<"operator =="<<endl;
    return true;
    }
    >
    };
    int main()
    {
    A<intb(10); //This would be OK, why?
    if(b==10){cout< <"success\n"; } //Why this can not run?
    return 0;
    }
    >
    I see in main()
    L1, 10 is an integer it implicitly transfer to class a
    L2, 10 is also an integer, but it can not implicitly transfer.
    why L1 can do this but L2 can not?
    Top-level issue seems to be the declaration of operator== as private.

    I haven't compiled the code, so may be more issues.

    Cheers, & hth.,

    - Alf

    Comment

    • Alf P. Steinbach

      #3
      Re: Would u explain this code about template and class

      * Alf P. Steinbach:
      * Lippman.Gan@gma il.com:
      >Sorry for that I can not clarify the type of issue in topic.
      >>
      >Look the code below:
      >>
      >template <class T>
      >class A
      >{
      > public:
      > class a
      > {
      > public:
      > a(int elem):x(elem){c out<<"a::a()\n" ;}
      > ~a(){cout<<"a:: ~a()\n";}
      > int GetX(){return x;};
      > private:
      > int x;
      > };
      > A(a _a1):a1(_a1){co ut<<"haha"<<end l;}
      > private:
      > a a1;
      > friend bool operator ==(const A<int>& t1,const A<int>& t2)
      > {
      > cout<<"operator =="<<endl;
      > return true;
      > }
      >>
      >};
      >int main()
      >{
      > A<intb(10); //This would be OK, why?
      > if(b==10){cout< <"success\n"; } //Why this can not run?
      > return 0;
      >}
      >>
      >I see in main()
      >L1, 10 is an integer it implicitly transfer to class a
      >L2, 10 is also an integer, but it can not implicitly transfer.
      >why L1 can do this but L2 can not?
      >
      Top-level issue seems to be the declaration of operator== as private.
      >
      I haven't compiled the code, so may be more issues.
      Uh, wait, I didn't see the "friend" there.

      This requires more thinking.

      Someone else, not me! (Busy!)

      Cheers, & sorry this doesn't help you,

      - Alf

      Comment

      • Barry

        #4
        Re: Would u explain this code about template and class

        Lippman.Gan@gma il.com wrote:
        Sorry for that I can not clarify the type of issue in topic.
        >
        Look the code below:
        >
        template <class T>
        class A
        {
        public:
        class a
        {
        public:
        a(int elem):x(elem){c out<<"a::a()\n" ;}
        ~a(){cout<<"a:: ~a()\n";}
        int GetX(){return x;};
        private:
        int x;
        };
        A(a _a1):a1(_a1){co ut<<"haha"<<end l;}
        private:
        a a1;
        friend bool operator ==(const A<int>& t1,const A<int>& t2)
        {
        cout<<"operator =="<<endl;
        return true;
        }
        >
        };
        int main()
        {
        A<intb(10); //This would be OK, why?
        if(b==10){cout< <"success\n"; } //Why this can not run?
        return 0;
        }
        >
        I see in main()
        L1, 10 is an integer it implicitly transfer to class a
        L2, 10 is also an integer, but it can not implicitly transfer.
        why L1 can do this but L2 can not?
        >
        I guess "no implicit type conversion" in a friend function.


        --
        Thanks
        Barry

        Comment

        • Michal Nazarewicz

          #5
          Re: Would u explain this code about template and class

          (I have modified Lippman's code a bit so that it is a full program which
          compiles fine.)

          Lippman.Gan@gma il.com writes:
          #include <iostream>
          >
          template <class Tclass A {
          public:
          class a {
          int x;
          public:
          a(int elem) : x(elem) { std::cout << "a::a()\n"; }
          ~a() { std::cout << "a::~a()\n" ; }
          int GetX() const { return x; }
          };
          A(a _a1) : a1(_a1) { std::cout << "haha\n"; }
          int GetX() const { return a1.GetX(); }
          private:
          a a1;
          };
          >
          bool operator==(cons t A<int&t1, const A<int&t2) {
          std::cout << "operator== \n";
          return t1.GetX() == t2.GetX();
          }
          >
          int main() {
          A<intb(10); //L1: This would be OK, why?
          if (b==10) std::cout << "success\n" ; //L2: Why this can not run?
          return 0;
          }
          >
          I see in main()
          L1, 10 is an integer it implicitly transfer to class a
          L2, 10 is also an integer, but it can not implicitly transfer.
          why L1 can do this but L2 can not?
          I would say that in L1 there is a direct conversion from int to
          A<int>::a (ie. A<int>::a::a(in t) constructor), whereas in L2 to convert
          int into A<intyou need two conversions: first convert int into
          A<int>::a and then convert it into A<int>. This may be the reason

          AFAIK if you want L1 not to compile you can use "explicit" keyword
          before a(int) declaration.

          --
          Best regards, _ _
          .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
          ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
          ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--

          Comment

          • Barry

            #6
            Re: Would u explain this code about template and class

            Barry wrote:
            Lippman.Gan@gma il.com wrote:
            >
            I guess "no implicit type conversion" in a friend function.
            >
            >
            My guess is wrong,
            Sorry

            --
            Thanks
            Barry

            Comment

            • D. Susman

              #7
              Re: Would u explain this code about template and class

              I guess what is wrong with this code is, in the "==" function you're
              trying to implicitly cast an int to type class A. In L1, it is valid
              because class a is constructed with an int. Since class A is
              constructed with a "class a" variable, implicit conversion request
              from int to class A is not sound.
              Maybe you anticipated for something like "cascaded implicit type
              conversion"? Even if it was implemented as a language feature, this
              would lead to being typeless in conversions as hierarchies go deep
              which would break down a well typed language.


              Comment

              • James Kanze

                #8
                Re: Would u explain this code about template and class

                On Sep 11, 12:54 pm, Michal Nazarewicz <min...@tlen.pl wrote:
                (I have modified Lippman's code a bit so that it is a full program which
                compiles fine.)
                Lippman....@gma il.com writes:
                #include <iostream>
                template <class Tclass A {
                public:
                class a {
                int x;
                public:
                a(int elem) : x(elem) { std::cout << "a::a()\n"; }
                ~a() { std::cout << "a::~a()\n" ; }
                int GetX() const { return x; }
                };
                A(a _a1) : a1(_a1) { std::cout << "haha\n"; }
                int GetX() const { return a1.GetX(); }
                private:
                a a1;
                };
                bool operator==(cons t A<int&t1, const A<int&t2) {
                std::cout << "operator== \n";
                return t1.GetX() == t2.GetX();
                }
                int main() {
                A<intb(10); //L1: This would be OK, why?
                if (b==10) std::cout << "success\n" ; //L2: Why this can not run?
                return 0;
                }
                I see in main()
                L1, 10 is an integer it implicitly transfer to class a
                L2, 10 is also an integer, but it can not implicitly transfer.
                why L1 can do this but L2 can not?
                I would say that in L1 there is a direct conversion from int to
                A<int>::a (ie. A<int>::a::a(in t) constructor), whereas in L2 to convert
                int into A<intyou need two conversions: first convert int into
                A<int>::a and then convert it into A<int>. This may be the reason
                Exactly. In the case of L1, there is an implicit conversion int
                to A<int>::a, and the A<int>::a is used to initialize the
                variable. One user defined conversion, so OK. In the case of
                L2, we need an A<int>, and it would require two user defined
                conversions (int to A<int>::a, then A<int>::a to A<intto get
                there), so it is illegal. Note that:

                A<intb = 10 ;

                would be illegal too, since copy initialization requires
                converting the initialization expression to the target type
                (here, A<int>).
                AFAIK if you want L1 not to compile you can use "explicit" keyword
                before a(int) declaration.
                And if he wants L2 to compile, he should add a constructor to A
                which takes a T.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                Working...