class Widget
{
public:
explicit Widget(int x = 0) {}
};
Widget wObject;
doSomething(wObject);
doSomething(28); //error
class Widget
{
public:
Widget(); //Default Constructor
Widget(const Widget& rhs); //Copy Constructor
Widget& operator=(const Widget& rhs); // Copy substitution oprerator
};
w1 = w2 // Copy substitution oprerator call!
Widget w3 = w2 //Copy Constructor call!
Widget w;
if (hasAcceptableQuality(w)) // Copy Constructor Call
함수 객체는 객체가 함수처럼 동작한다 하여 함수 객체라고 할 수 있는데 우리가 전에 배웠던 연산자 오버로딩에서 ()라는 연산자를 오버로딩하여 객체를 함수처럼 쓸 수 있습니다.
#include <iostream>
using namespace std;
class Plus
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
int main()
{
Plus pls;
cout << "pls(10, 20): " << pls(10, 20) << endl;
return 0;
}
C++는 실행 시간에 어떤 현상이 터질지 확실히 예측할 수 없다.
#include<iostream>
using namespace std;
int* p = 0;
int name[] = { 1, 2, 3};
int main(void)
{
int a = 5;
cout << name[a] << endl;
}
다음과 같은 코드를 실행하면 에러가 발생하는 것이 아니라 통과된다. 에러가 발생하지 않기 때문에 어떤 동작이 발생할 지 모른다는 문제점이 있다.
클래스는 속성, 메서드, 이벤트, 인덱서 등을 갖지만, 인터페이스는 이를 직접 구현하지 않고 단지 정의(prototype definition)만을 갖는다. C++는 인터페이스가 없지만 다음과 같이 추상클래스를 이용하여 사용할 수 있다.
class ICamera
{
public:
virtual void take() = 0;
virtual void print() = 0;
};