Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Call to a constructor while executing (obj < 3) . Kindly explain?

Call to a constructor while executing (obj < 3) . Kindly explain?

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3971400
    wrote on last edited by
    #1

    class CA{
    private:
    int m_Var;
    public:
    CA(int x=0): m_Var(x){cout<<"1";}
    bool operator < (const CA &obj2) { cout<<"2"; return (m_Var < obj2.m_Var) ? true : false;}
    };
    int main(void) {
    CA obj1=2;
    if(obj1 < 3){cout << "In If block" << endl;}
    }

    the output of this code is 112In If block .. i can understand the reason behind the first 1 and "2In If block" .. i dont understand when the second 1 is being called? as per my understanding when (obj1<3) executes it may call obj1.operator<(3) But constructor is being called but whats makes it to call? kindly explain why the second 1 is being displayed?

    N N 2 Replies Last reply
    0
    • U User 3971400

      class CA{
      private:
      int m_Var;
      public:
      CA(int x=0): m_Var(x){cout<<"1";}
      bool operator < (const CA &obj2) { cout<<"2"; return (m_Var < obj2.m_Var) ? true : false;}
      };
      int main(void) {
      CA obj1=2;
      if(obj1 < 3){cout << "In If block" << endl;}
      }

      the output of this code is 112In If block .. i can understand the reason behind the first 1 and "2In If block" .. i dont understand when the second 1 is being called? as per my understanding when (obj1<3) executes it may call obj1.operator<(3) But constructor is being called but whats makes it to call? kindly explain why the second 1 is being displayed?

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      Member 3974347 wrote:

      as per my understanding when (obj1<3) executes it may call obj1.operator<(3)

      Not actually. It is actually called like obj1.operator<( CA(3));// A local temporary object is created here. Here the integer 3 is automatically converted to a CA object by involking the constructor. Such conversions are called implicit conversions. You can prevent such conversions using the explicit keyword[^]

      nave [OpenedFileFinder] [My Blog]

      N U 2 Replies Last reply
      0
      • U User 3971400

        class CA{
        private:
        int m_Var;
        public:
        CA(int x=0): m_Var(x){cout<<"1";}
        bool operator < (const CA &obj2) { cout<<"2"; return (m_Var < obj2.m_Var) ? true : false;}
        };
        int main(void) {
        CA obj1=2;
        if(obj1 < 3){cout << "In If block" << endl;}
        }

        the output of this code is 112In If block .. i can understand the reason behind the first 1 and "2In If block" .. i dont understand when the second 1 is being called? as per my understanding when (obj1<3) executes it may call obj1.operator<(3) But constructor is being called but whats makes it to call? kindly explain why the second 1 is being displayed?

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Member 3974347 wrote:

        But constructor is being called but whats makes it to call?

        Because constructor accepts a int.

        Member 3974347 wrote:

        as per my understanding when (obj1<3) executes it may call obj1.operator<(3)

        Your operator overload accepts const CA & but you specified the expression obj1<3, since type CA has a constructor which takes int, a temporary will be created by passing 3 to the constructor of CA and this is the reason for the second time constructor call. Use explicit keyword to avoid the confusion. :)

        Navaneeth How to use google | Ask smart questions

        U 1 Reply Last reply
        0
        • N Naveen

          Member 3974347 wrote:

          as per my understanding when (obj1<3) executes it may call obj1.operator<(3)

          Not actually. It is actually called like obj1.operator<( CA(3));// A local temporary object is created here. Here the integer 3 is automatically converted to a CA object by involking the constructor. Such conversions are called implicit conversions. You can prevent such conversions using the explicit keyword[^]

          nave [OpenedFileFinder] [My Blog]

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          You are so fast! :-D

          Navaneeth How to use google | Ask smart questions

          N 1 Reply Last reply
          0
          • N N a v a n e e t h

            You are so fast! :-D

            Navaneeth How to use google | Ask smart questions

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            Well, I borrowed some monkeys[^] from Rajesh. :-D

            nave [OpenedFileFinder] [My Blog]

            1 Reply Last reply
            0
            • N Naveen

              Member 3974347 wrote:

              as per my understanding when (obj1<3) executes it may call obj1.operator<(3)

              Not actually. It is actually called like obj1.operator<( CA(3));// A local temporary object is created here. Here the integer 3 is automatically converted to a CA object by involking the constructor. Such conversions are called implicit conversions. You can prevent such conversions using the explicit keyword[^]

              nave [OpenedFileFinder] [My Blog]

              U Offline
              U Offline
              User 3971400
              wrote on last edited by
              #6

              Thanks. :)

              1 Reply Last reply
              0
              • N N a v a n e e t h

                Member 3974347 wrote:

                But constructor is being called but whats makes it to call?

                Because constructor accepts a int.

                Member 3974347 wrote:

                as per my understanding when (obj1<3) executes it may call obj1.operator<(3)

                Your operator overload accepts const CA & but you specified the expression obj1<3, since type CA has a constructor which takes int, a temporary will be created by passing 3 to the constructor of CA and this is the reason for the second time constructor call. Use explicit keyword to avoid the confusion. :)

                Navaneeth How to use google | Ask smart questions

                U Offline
                U Offline
                User 3971400
                wrote on last edited by
                #7

                Thanks :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups