C++ 생성자, 컴파일러

조회수 1933회
 #include <iostream> 
using namespace std; 
class Point { 
   private: 
   float X; float Y; 
   static int count; 
   public: 
   Point() {}; 
   Point(float a) : X(a), Y(a) { ++count; } 
   Point(const Point& a); 
   static void printCount() { 
      cout << "Count = " << count << endl; 
   } 
   void printValues() { cout << "(" << X << "," << Y << ")" << endl; } 
   friend Point operator+(const Point& a, const Point& b); 
}; 
Point operator+(const Point& a, const Point& b) { 
   Point temp; 
   temp.X = a.X + b.X; 
   temp.Y = a.Y + b.Y; 
   return temp; 
} 
Point::Point(const Point& a) { 
   X = a.X; Y = a.Y; 
   ++count; 
} 
int Point::count = 0; 
int main() { 
   Point myPoint(1.0); 
   Point yourPoint(2.0); 
   (myPoint + yourPoint).printValues(); 
   Point::printCount(); 
   return 0; 
}

생성자를 호출하는 갯수를 카운트 하는 코드입니다

제 생각에는 myPoint생성할때 한번 yourPoint생성할때 한번, operator+에서 리턴할때 한번 그래서 count = 3이 되어야 한다고 생각하는데,

gcc컴파일러를 쓰는 Codeblocks에서는 2번으로 출력되고 visual studio 에서는 3번이라고 출력되더군요.. 둘중에 어떤차이인가요..?

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)