why Visual Studio can not optimize the initialization code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • George2
    New Member
    • Dec 2007
    • 200

    why Visual Studio can not optimize the initialization code?

    Hello everyone,


    Why Visual Studio compiler can not optimize in this case? I think this case is almost the same as sample 1, why compiler can optimize sample 1 but can not optimze sample 2?

    (sample 2, http://msdn2.microsoft .com/en-us/library/ms364057(vs.80) .aspx)

    Code:
    #include <stdio.h>
    class A {
      public:
        A() {printf ("A: I am in constructor\n");i = 1;}
        ~A() { printf ("A: I am in destructor\n"); i = 0;}
        A(const A& a) {printf ("A: I am in copy constructor\n"); i = a.i;}
        int i, x, w;
    };
     class B {
      public:
        A a;
        B()  { printf ("B: I am in constructor\n");}
        ~B() { printf ("B: I am in destructor\n");}
        B(const B& b) { printf ("B: I am in copy constructor\n");}
    };
    A MyMethod()
    {
        B* b = new B();
        A a = b->a;
        delete b;
        return (a);
    }
    int main()
    {
        A a;
        a = MyMethod();
    }

    thanks in advance,
    George
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Optimization is an advanced topic. You need no be concerned with this until after your C++ program has been coded and debugged. And maybe not even then.

    I have been using C++ for 17 years and have not yet needed to optimize. My applications just didn't warrant it.

    I would post this question again when you have about 5 yrs C++ experience.

    Comment

    • George2
      New Member
      • Dec 2007
      • 200

      #3
      Hi weaknessforcats ,


      I have programmed with C++ for more than 5 years, including the time in school. Any ideas for the question. :-)

      Originally posted by weaknessforcats
      Optimization is an advanced topic. You need no be concerned with this until after your C++ program has been coded and debugged. And maybe not even then.

      I have been using C++ for 17 years and have not yet needed to optimize. My applications just didn't warrant it.

      I would post this question again when you have about 5 yrs C++ experience.

      regards,
      George

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        In that case, start here: http://msdn2.microsoft.com/en-us/lib...e8(VS.71).aspx.

        Comment

        • George2
          New Member
          • Dec 2007
          • 200

          #5
          Thanks for your advice, weaknessforcats !


          Originally posted by weaknessforcats

          regards,
          George

          Comment

          Working...