Connecting Tech Pros Worldwide Help | Site Map

why Visual Studio can not optimize the initialization code?

Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#1: Dec 19 '07
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)

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. class A {
  3.   public:
  4.     A() {printf ("A: I am in constructor\n");i = 1;}
  5.     ~A() { printf ("A: I am in destructor\n"); i = 0;}
  6.     A(const A& a) {printf ("A: I am in copy constructor\n"); i = a.i;}
  7.     int i, x, w;
  8. };
  9.  class B {
  10.   public:
  11.     A a;
  12.     B()  { printf ("B: I am in constructor\n");}
  13.     ~B() { printf ("B: I am in destructor\n");}
  14.     B(const B& b) { printf ("B: I am in copy constructor\n");}
  15. };
  16. A MyMethod()
  17. {
  18.     B* b = new B();
  19.     A a = b->a;
  20.     delete b;
  21.     return (a);
  22. }
  23. int main()
  24. {
  25.     A a;
  26.     a = MyMethod();
  27. }
  28.  

thanks in advance,
George
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Dec 19 '07

re: why Visual Studio can not optimize the initialization code?


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.
Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#3: Dec 20 '07

re: why Visual Studio can not optimize the initialization code?


Hi weaknessforcats,


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

Quote:

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
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#4: Dec 20 '07

re: why Visual Studio can not optimize the initialization code?


In that case, start here: http://msdn2.microsoft.com/en-us/lib...e8(VS.71).aspx.
Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#5: Dec 21 '07

re: why Visual Studio can not optimize the initialization code?


Thanks for your advice, weaknessforcats!


Quote:

Originally Posted by weaknessforcats

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


regards,
George
Reply