This article is rather long. If you want to know about both aggregates and PODs (Plain Old Data) take time and read it. If you are interested just in aggregates, read only the first part. If you are interested only in PODs then you must first read the definition, implications, and examples of aggregates and then you may jump to PODs but I would still recommend reading the first part in its entirety. The notion of aggregates is essential for defining PODs. If you find any errors (even minor, including grammar, stylistics, formatting, syntax, etc.) please leave a comment, I'll edit.

What are aggregates and why they are special

Formal definition from the C++ standard (C++03 8.5.1 §1):

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).

So, OK, let's parse this definition. First of all, any array is an aggregate. A class can also be an aggregate if… wait! nothing is said about structs or unions, can't they be aggregates? Yes, they can. In C++, the term class refers to all classes, structs, and unions. So, a class (or struct, or union) is an aggregate if and only if it satisfies the criteria from the above definitions. What do these criteria imply?

  • This does not mean an aggregate class cannot have constructors, in fact it can have a default constructor and/or a copy constructor as long as they are implicitly declared by the compiler, and not explicitly by the user

  • No private or protected non-static data members. You can have as many private and protected member functions (but not constructors) as well as as many private or protected static data members and member functions as you like and not violate the rules for aggregate classes

  • An aggregate class can have a user-declared/user-defined copy-assignment operator and/or destructor

  • An array is an aggregate even if it is an array of non-aggregate class type.

Now let's look at some examples:

class NotAggregate1
{
  virtual void f() {} //remember? no virtual functions
};

class NotAggregate2
{
  int x; //x is private by default and non-static 
};

class NotAggregate3
{
public:
  NotAggregate3(int) {} //oops, user-defined constructor
};

class Aggregate1
{
public:
  NotAggregate1 member1;   //ok, public member
  Aggregate1& operator=(Aggregate1 const & rhs) {/* */} //ok, copy-assignment  
private:
  void f() {} // ok, just a private function
};

You get the idea. Now let's see how aggregates are special. They, unlike non-aggregate classes, can be initialized with curly braces {}. This initialization syntax is commonly known for arrays, and we just learnt that these are aggregates. So, let's start with them.

Type array_name[n] = {a1, a2, …, am};

if(m == n)
the ith element of the array is initialized with ai
else if(m < n)
the first m elements of the array are initialized with a1, a2, …, am and the other n - m elements are, if possible, value-initialized (see below for the explanation of the term)
else if(m > n)
the compiler will issue an error
else (this is the case when n isn't specified at all like int a[] = {1, 2, 3};)
the size of the array (n) is assumed to be equal to m, so int a[] = {1, 2, 3}; is equivalent to int a[3] = {1, 2, 3};

When an object of scalar type (boolintchardouble, pointers, etc.) is value-initialized it means it is initialized with 0 for that type (false for bool0.0 for double, etc.). When an object of class type with a user-declared default constructor is value-initialized its default constructor is called. If the default constructor is implicitly defined then all nonstatic members are recursively value-initialized. This definition is imprecise and a bit incorrect but it should give you the basic idea. A reference cannot be value-initialized. Value-initialization for a non-aggregate class can fail if, for example, the class has no appropriate default constructor.

Examples of array initialization:

class A
{
public:
  A(int) {} //no default constructor
};
class B
{
public:
  B() {} //default constructor available
};
int main()
{
  A a1[3] = {A(2), A(1), A(14)}; //OK n == m
  A a2[3] = {A(2)}; //ERROR A has no default constructor. Unable to value-initialize a2[1] and a2[2]
  B b1[3] = {B()}; //OK b1[1] and b1[2] are value initialized, in this case with the default-ctor
  int Array1[1000] = {0}; //All elements are initialized with 0;
  int Array2[1000] = {1}; //Attention: only the first element is 1, the rest are 0;
  bool Array3[1000] = {}; //the braces can be empty too. All elements initialized with false
  int Array4[1000]; //no initializer. This is different from an empty {} initializer in that
  //the elements in this case are not value-initialized, but have indeterminate values 
  //(unless, of course, Array4 is a global array)
  int array[2] = {1, 2, 3, 4}; //ERROR, too many initializers
}

Now let's see how aggregate classes can be initialized with braces. Pretty much the same way. Instead of the array elements we will initialize the non-static data members in the order of their appearance in the class definition (they are all public by definition). If there are fewer initializers than members, the rest are value-initialized. If it is impossible to value-initialize one of the members which were not explicitly initialized, we get a compile-time error. If there are more initializers than necessary, we get a compile-time error as well.

struct X
{
  int i1;
  int i2;
};
struct Y
{
  char c;
  X x;
  int i[2];
  float f; 
protected:
  static double d;
private:
  void g(){}      
}; 

Y y = {'a', {10, 20}, {20, 30}};

In the above example y.c is initialized with 'a'y.x.i1 with 10y.x.i2 with 20y.i[0] with 20y.i[1] with 30 and y.f is value-initialized, that is, initialized with 0.0. The protected static member d is not initialized at all, because it is static.

Aggregate unions are different in that you may initialize only their first member with braces. I think that if you are advanced enough in C++ to even consider using unions (their use may be very dangerous and must be thought of carefully), you could look up the rules for unions in the standard yourself :).

Now that we know what's special about aggregates, let's try to understand the restrictions on classes; that is, why they are there. We should understand that memberwise initialization with braces implies that the class is nothing more than the sum of its members. If a user-defined constructor is present, it means that the user needs to do some extra work to initialize the members therefore brace initialization would be incorrect. If virtual functions are present, it means that the objects of this class have (on most implementations) a pointer to the so-called vtable of the class, which is set in the constructor, so brace-initialization would be insufficient. You could figure out the rest of the restrictions in a similar manner as an exercise :).

So enough about the aggregates. Now we can define a stricter set of types, to wit, PODs

What are PODs and why they are special

Formal definition from the C++ standard (C++03 9 §4):

A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.

Wow, this one's tougher to parse, isn't it? :) Let's leave unions out (on the same grounds as above) and rephrase in a bit clearer way:

An aggregate class is called a POD if it has no user-defined copy-assignment operator and destructor and none of its nonstatic members is a non-POD class, array of non-POD, or a reference.

What does this definition imply? (Did I mention POD stands for Plain Old Data?)

  • All POD classes are aggregates, or, to put it the other way around, if a class is not an aggregate then it is sure not a POD
  • Classes, just like structs, can be PODs even though the standard term is POD-struct for both cases
  • Just like in the case of aggregates, it doesn't matter what static members the class has

Examples:

struct POD
{
  int x;
  char y;
  void f() {} //no harm if there's a function
  static std::vector<char> v; //static members do not matter
};

struct AggregateButNotPOD1
{
  int x;
  ~AggregateButNotPOD1() {} //user-defined destructor
};

struct AggregateButNotPOD2
{
  AggregateButNotPOD1 arrOfNonPod[3]; //array of non-POD class
};

POD-classes, POD-unions, scalar types, and arrays of such types are collectively called POD-types.
PODs are special in many ways. I'll provide just some examples.

  • POD-classes are the closest to C structs. Unlike them, PODs can have member functions and arbitrary static members, but neither of these two change the memory layout of the object. So if you want to write a more or less portable dynamic library that can be used from C and even .NET, you should try to make all your exported functions take and return only parameters of POD-types.

  • The lifetime of objects of non-POD class type begins when the constructor has finished and ends when the destructor has finished. For POD classes, the lifetime begins when storage for the object is occupied and finishes when that storage is released or reused.

  • For objects of POD types it is guaranteed by the standard that when you memcpy the contents of your object into an array of char or unsigned char, and then memcpy the contents back into your object, the object will hold its original value. Do note that there is no such guarantee for objects of non-POD types. Also, you can safely copy POD objects with memcpy. The following example assumes T is a POD-type:

    #define N sizeof(T)
    char buf[N];
    T obj; // obj initialized to its original value
    memcpy(buf, &obj, N); // between these two calls to memcpy,
    // obj might be modified
    memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar type
    // holds its original value
  • goto statement. As you may know, it is illegal (the compiler should issue an error) to make a jump via goto from a point where some variable was not yet in scope to a point where it is already in scope. This restriction applies only if the variable is of non-POD type. In the following example f() is ill-formed whereas g() is well-formed. Note that Microsoft's compiler is too liberal with this rule—it just issues a warning in both cases.

    int f()
    {
      struct NonPOD {NonPOD() {}};
      goto label;
      NonPOD x;
    label:
      return 0;
    }
    
    int g()
    {
      struct POD {int i; char c;};
      goto label;
      POD x;
    label:
      return 0;
    }
  • It is guaranteed that there will be no padding in the beginning of a POD object. In other words, if a POD-class A's first member is of type T, you can safely reinterpret_cast from A* to T*and get the pointer to the first member and vice versa.

The list goes on and on…

Conclusion

It is important to understand what exactly a POD is because many language features, as you see, behave differently for them.

'C++ > C++ 11 14 17' 카테고리의 다른 글

C++ 11 ENUM  (0) 2018.04.11
auto keyword  (0) 2018.04.05
함수포인터, std::function  (0) 2017.04.19
R 레퍼런스 타입.  (0) 2017.01.30
Modern C++ [MS]  (0) 2016.12.28

https://www.codeproject.com/Articles/522094/Outlook-add-in-using-VCplusplus-ATL-COM

' > URL' 카테고리의 다른 글

펌] 각 언어별 딥러닝~ 라이브러리.  (0) 2017.01.17
CUDA  (0) 2016.12.12
GIT 튜토리얼.  (0) 2016.12.08
NVIDIA, CUDA  (0) 2016.11.23

MFC로 프로그래밍을 하다 보면, 각 클래스에 어떻게 접근해서 포인터를 얻어와야 하는지 매우 어려울 때가 많다...

특히 초중급자 시절엔 말이다.. 사용한 지 오래 되어도 여전히 헷갈리기는 매 한가지다.

자 그럼.. 하나씩.. 이해해 보자.


우선 MFC로 프로그램을 만들면, 다음과 같이 클래스가 생성된다. 프로젝트 명을 Test라고 가정해 보자.


CTestApp - CWinApp 클래스를 상속, 프로그램 초기화 클래스 (InitInstance)

CMainFrame - CFrameWnd 클래스를 상속, 전체 윈도우 관련 클래스(툴바, 메뉴, 상태바 등등)

CTestDoc - CDocument 클래스를 상속, 문서 관련 클래스(Open, Save, Serialize)

CTestView - CView 클래스를 상속, 사용자 화면 클래스(OnPaint, OnDraw)

CAboutDlg - CDialog 클래스를 상속, 도움말 대화 상자 클래스(DoModal)


-------------------------------------------------*^^*--------------------------------------------------


우선 가장 쉬운 것부터 설명하자면,

어느 곳에서나 CTestApp의 포인터를 얻고자 한다면, 다음과 같이 코딩한다.

AfxGetApp()는 전역 함수이므로 어느 곳에서나 호출할 수 있다.


CTestApp* pApp = AfxGetApp();


하지만 위와 같이 사용하면, 다음과 같은 에러가 발생한다.


error C2440: 'initializing' : cannot convert from 'class CWinApp *' to 'class CTestApp *'

에러를 보면, CWinApp*를 CTestApp*로 변환할 수 없다는 것인데, AfxGetApp()의 함수 원형이


CWinApp* AfxGetApp();


이기 때문이다. 그래서 이 문제를 해결하기 위해서는 아래와 같이 형변환(casting)을 해 주어야 한다.


CTestApp* pApp = (CTestApp*)AfxGetApp();


이제는 컴파일 에러 없이 잘 될 것이다. 그리고 다음과 같은 에러가 발생될 수도 있느데,


error C2065: 'CTestApp' : undeclared identifier


이는 CTestApp 클래스의 선언 부분이 포함(include)되지 않았기 때문이다. 이럴 때는

CTestApp 클래스의 선언을 다음처럼 포함시켜야 한다.


#include "Test.h"


pApp는 이미 생성되어 있는 CTestApp의 객체 포인터이기 때문에,

이젠 pApp 포인터를 통해 CTestApp 객체에 쉽게 접근할 수 있다. 만약에 CTestApp에


int i;


라는 멤버 변수가 있었다면,


pApp->i = 5;


와 같이 사용할 수 있다. 또한 멤버 함수 func()가 있다면,


pApp->func();


처럼 접근할 수 있다. 주의해야 할 것은 객체 접근이기 때문에 public 속성의 멤버들만 접근할 수 있고,

private와 protected는 접근할 수 없음에 유의해야 한다.


-------------------------------------------------*^^*--------------------------------------------------


두 번째는 CMainFrame의 포인터을 얻어 오는 방법이다. 다음과 같이 사용하려 할 때,


CMainFrame* pFrame = AfxGetMainWnd();


두 가지 에러와 만나게 된다.

첫 번째, CMainFrame에 대해서 컴파일러가 모르겠다는 에러이다.


error C2065: 'CMainFrame' : undeclared identifier

이 에러를 수정하기 위해서는 CMainFrame 클래스가 선언되어 있는 헤더 파일을

다음처럼 포함시키면 된다. 그리고 포함시키는 위치도 Test.h와 TestDoc.h 사이가 좋다.

그것은 클래스들간에 서로 관계가 있기 때문에 포함 순서는 매우 중요하다.


#include "stdafx.h"
#include "Test.h"


#include "MainFrm.h"


#include "TestDoc.h"
#include "TestView.h"

이 에러를 해결하고 나면, 다음과 같이 CTestApp에서 발생했던 에러가 발생한다.


error C2440: 'initializing' : cannot convert from 'class CWnd *' to 'class CMainFrame *'

에러를 보면, CWnd*를 CMainFrame*로 변환할 수 없다는 것인데, AfxGetMainWnd()의 함수 원형이


CWnd* AfxGetMainWnd();


이기 때문이다. 그래서 이 문제를 해결하기 위해서는 아래와 같이 형변환(casting)을 해 주어야 한다.


CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();


이제는 컴파일 에러 없이 잘 될 것이다. CMainFrame은 CFrameWnd를 상속 받고, CFrameWnd는

CWnd를 상속 받기 때문에, pFrame 포인터를 통해서 CFrameWnd와 CWnd가 갖고 있는 모든 함수를

호출하여 사용할 수 있다. 만약 윈도우의 타이틀에 나오는 제목을 바꾸고 싶다면,


pFrame->SetWindowText( "야.. 바뀌어라" );


처럼 사용하면 된다.


-------------------------------------------------*^^*--------------------------------------------------


CMainFrame*는 또 다른 이유로 자주 사용하게 되는데, 그 이유는 도큐먼트(CTestDoc)와

뷰(CTestView) 클래스에 접근하기 위해서이다. CMainFrame 클래스는 뷰에 접근하는 GetActiveView(); 함수와

문서에 접근하는 GetActiveDocument() 함수를 제공한다.


우리가 어느 특정 클래스를 만들어서 사용하고, 이 클래스가 뷰에 접근해야 한다면 다음과 같은 문장을 사용해야 한다.


CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();

CTestView* pView = (CTestView*)pFrame->GetActiveView();


만약 도큐먼트 클래스에 접근해야 한다면,


CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();

CTestDoc* pDoc = (CTestDoc*)pFrame->GetActiveDocument();

 

물론 이 때는 각각의 클래스에 대한 헤더를 반드시 포함시켜야 함을 잊지 말아야 된다.

가끔 아래와 같은 순서로 헤더를 포함시키지 않는 경우가 있는데, 반드시 아래와 같이 포함해야 한다.


#include "MainFrm.h"

#include "TestDoc.h"

#include "TestView.h"


만약 대화 상자에서 도큐먼트 클래스에 접근하려면 어떻게 할 것인가? 방법은 위와 동일하다.


-------------------------------------------------*^^*--------------------------------------------------

 

다음은 뷰에서 도큐먼트에 접근하는 것과 도큐먼트에서 뷰에 접근하는 것에 대해 알아보자.

뷰와 도큐먼트는 서로를 직접 접근할 수 있는 함수가 제공된다. 뷰에서 도큐먼트를 직접 접근하려면,

 

CTestDoc* pDocument = (CTestDoc*)GetDocument();

 

를 호출하면 된다. 물론 View 클래스에는 GetDocument() 함수가 존재해야 한다. 일반적으로 이 함수는

MFC로 프로젝트 생성 시 기본적으로 있다. 하지만 만약 직접 만든 새로운 클래스라면 GetDocument() 함수를

직접 만들어줘야 한다. 직접 만들기가 귀찮다면, 다음과 같이 사용해도 된다.

 

CTestDoc* pDocument = (CTestDoc*)m_pDocument;

 

m_pDocument는 CTestView 클래스의 멤버이며, 다음과 같이 선언되어 있다.

 

CDocument* m_pDocument;


그리고, 도큐먼트에 뷰 클래스를 참조하려면 아래와 같이 해야 한다.


POSITION pos = GetFirstViewPosition();
CTestView* pView = (CTestView*)GetNextView( pos );

도큐먼트는 뷰 클래스를 내부적으로 링크드리스트를 사용해서 관리하고 있다. 그러므로 GetFirstViewPosition()

함수를 통해 POSITION을 얻어 온 다음, GetNextView() 함수를 통해 뷰의 포인터를 얻어오면 된다.

만약 창 분할에 의해 뷰가 여러 개 존재한다면, 다음과 같이 얻어 올 수 있다. 만약 컴파일 에러가 발생하면,

#include "TestView.h" 처럼 해서 헤더 파일을 포함하는 것도 잊지 말자.


POSITION pos = GetFirstViewPosition();
CTestView1* pView1 = (CTestView1*)GetNextView( pos );

CTestView2* pView2 = (CTestView2*)GetNextView( pos );
CTestView3* pView3 = (CTestView3*)GetNextView( pos );
CTestView4* pView4 = (CTestView4*)GetNextView( pos );
...


-------------------------------------------------*^^*--------------------------------------------------

 

그렇다면,,, CTestApp 클래스에서 뷰(CTestView) 클래스는 어떻게 얻어 올 수 있을까? 쉽게 응용할 수 있지만

머리가 꽉 막힐 수도 있다. 이 곳에서도 혹시 에러가 나면 헤더 파일을 자~알 추가해야 할 것이다.

 

CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
CTestView* pView = (CTestView*)pFrame->GetActiveView();


 

-------------------------------------------------*^^*--------------------------------------------------

 

마지막으로, 뷰(CTestView) 클래스에서 CMainFrame에 접근할 수 있는데 AfxGetMainWnd() 함수외에도

 

CMainFrame* pFrame = (CMainFrame*)GetParentFrame();

 

-------------------------------------------------*^^*--------------------------------------------------

 

MDI 환경에서 포인터를 가져와 보자.

 

CTestApp* pApp = (CTestApp*)AfxGetApp();

POSITION pos = pApp->GetFirstDocTemplatePosition();
    
CDocTemplate* pDocTemplate;
pDocTemplate = pApp->GetNextDocTemplate( pos );
  // 첫 번째 등록한 템플릿
pDocTemplate->OpenDocumentFile( NULL );               // 첫 번째 문서 생성

 

POSITION posDoc = pDocTemplate->GetFirstDocPosition();
CTestDoc* pDoc = (CTestDoc*)pDocTemplate->GetNextDoc( posDoc );
  // 첫 번째 문서 포인터
ASSERT( pDoc->IsKindof( RUNTIME_CLASS(CMongDoc) ) );                   // 포인터 유효성 검사
//CXXXDoc* pDoc2 = (CXXXDoc*)pDocTemplate->GetNextDoc( posDoc );             // 두 번째 문서 포인터

POSITION posView = pDoc->GetFirstViewPosition();
CTestView* pView = (CTestView*)pDoc->GetNextView( posView );
       // 첫 번째 뷰 포인터
ASSERT( pView->IsKindof( RUNTIME_CLASS(CTestView) ) );                // 포인터 유효성 검사

//CXXXView* pView2 = (CXXXView*)pDoc->GetNextView( posView );                 // 두 번째 뷰 포인터
//ASSERT( pView2->IsKindof( RUNTIME_CLASS(CXXXView) ) );                         // 포인터 유효성 검사

// 다음 템플릿이 존재할 경우만 가능합니다.

pDocTemplate = pApp->GetNextDocTemplate( pos );  // 두 번째 등록한 템플릿
if( pDocTemplate )                                                         
// 두 번째 템플릿이 존재하는지 검사

{

    pDocTemplate->OpenDocumentFile( NULL );          // 두 번째 문서 생성

}

 

-------------------------------------------------*^^*--------------------------------------------------

 

분할 뷰에서는 위의 방법과 같이 포인터를 얻어 올 수 있다. 또는 다음과 같은 방법으로 얻어 올 수 있다.

만약 창이 두 개로 분할되어 있다면,

 

CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();

CTestView* pView1 = (CTestView*)pFrame->m_wndSplitter.GetPane(0,0);   // 첫 번째 뷰 포인터 얻기

CXXXView* pView2 = (CTestView*)pFrame->m_wndSplitter.GetPane(0,1);   // 두 번째 뷰 포인터 얻기


뷰 포인터는 다음과 같이 좌표를 사용하여 구할 수 있다.


---------------------------------------------

|                                |                               |

|      GetPane( 0, 0 )       |     GetPane( 0, 1 )       |

|                                |                               |

---------------------------------------------

|                                |                               |

|      GetPane( 1, 0 )    |     GetPane( 1, 1 )     |

|                                |                               |

--------------------------------------------------------------------

|                                |                               |                                 |

|      GetPane( 2, 0 )    |     GetPane( 2, 1 )     |     GetPane( 2, 2 )             |

|                                |                               |                                 |

--------------------------------------------------------------------


펌:http://egloos.zum.com/indra207/v/5175542


' > WIN32 MFC' 카테고리의 다른 글

파일 관련 API 정리.  (0) 2016.11.22

+ Recent posts