一个数据成员的初始化问题

arden1019 2004-10-22 09:45:17
在David J. Kruglinski写的那本programming vc++书中有这样一个例子:

EX04AVIEW.H
// ex04aView.h : interface of the CEx04aView class
//
///////////////////////////////////////////////////////////////////////

#if !defined(AFX_EX04AVIEW_H__B188BE41_6377_11D0_8FD4_00C04FC2A0C2
__INCLUDED_)
#define AFX_EX04AVIEW_H__B188BE41_6377_11D0_8FD4_00C04FC2A0C2
__INCLUDED_

#if _MFC_VER > 1000
#pragma once
#endif // _MFC_VER > 1000
class CEx04aView : public CView
{
protected: // create from serialization only
CEx04aView();
DECLARE_DYNCREATE(CEx04aView)

// Attributes
public:
CEx04aDoc* GetDocument();

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEx04aView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CEx04aView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
//{{AFX_MSG(CEx04aView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
int m_nColor;
CRect m_rectEllipse;
};
#ifndef _DEBUG // debug version in ex04aView.cpp
inline CEx04aDoc* CEx04aView::GetDocument()
{ return (CEx04aDoc*)m_pDocument; }
#endif

///////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations
// immediately before the previous line.

#endif // !defined(AFX_EX04AVIEW_H__B188BE41_6377_11D0_8FD4_00C04FC2A0C2__INCLUDED_)


EX04AVIEW.CPP
// ex04aView.cpp : implementation of the CEx04aView class
//

#include "stdafx.h"
#include "ex04a.h"

#include "ex04aDoc.h"
#include "ex04aView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE
__;
#endif

///////////////////////////////////////////////////////////////////////
// CEx04aView

IMPLEMENT_DYNCREATE(CEx04aView, CView)

BEGIN_MESSAGE_MAP(CEx04aView, CView)
//{{AFX_MSG_MAP(CEx04aView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////
// CEx04aView construction/destruction

CEx04aView::CEx04aView() : m_rectEllipse(0, 0, 200, 200)
{
m_nColor = GRAY_BRUSH;
}

CEx04aView::~CEx04aView()
{
}

BOOL CEx04aView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

///////////////////////////////////////////////////////////////////////
// CEx04aView drawing

void CEx04aView::OnDraw(CDC* pDC)
{
pDC->SelectStockObject(m_nColor);
pDC->Ellipse(m_rectEllipse);
}

///////////////////////////////////////////////////////////////////////
// CEx04aView printing

BOOL CEx04aView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CEx04aView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CEx04aView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

///////////////////////////////////////////////////////////////////////
// CEx04aView diagnostics

#ifdef _DEBUG
void CEx04aView::AssertValid() const
{
CView::AssertValid();
}

void CEx04aView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CEx04aDoc* CEx04aView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx04aDoc)));
return (CEx04aDoc*)m_pDocument;
}
#endif //_DEBUG

///////////////////////////////////////////////////////////////////////
// CEx04aView message handlers

void CEx04aView::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_rectEllipse.PtInRect(point)) {
if (m_nColor == GRAY_BRUSH) {
m_nColor = WHITE_BRUSH;
}
else {
m_nColor = GRAY_BRUSH;
}
InvalidateRect(m_rectEllipse);
}
}

我搞不明白在

CEx04aView::CEx04aView() : m_rectEllipse(0, 0, 200, 200)
{
m_nColor = GRAY_BRUSH;
}
中m_rectEllipse(0, 0, 200, 200)为什么要写在这个地方?写在括号里面不可以么?
...全文
89 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdleek 2004-10-22
  • 打赏
  • 举报
回复
楼主需要重新温习c++语法
mengjj 2004-10-22
  • 打赏
  • 举报
回复
如果你想用非默認構造函數初使化成員變量﹐就得如此
奔跑的北极熊 2004-10-22
  • 打赏
  • 举报
回复
构造函数的初始化列表,为了简单明了。
zhiguo_he 2004-10-22
  • 打赏
  • 举报
回复
你最好去看一下C++的语法
李马 2004-10-22
  • 打赏
  • 举报
回复
这是构造函数的语法,表示在CEx04aView::CEx04aView()构造的时候调用了CRect::CRect(int l, int t, int r, int b)来初始化m_rectEllipse。
当然,你也可以这么做:
CEx04aView::CEx04aView()
{
m_rectEllipse.SetRect(0, 0, 200, 200);
m_nColor = GRAY_BRUSH;
}
arden1019 2004-10-22
  • 打赏
  • 举报
回复
好沉,顶一个
arden1019 2004-10-22
  • 打赏
  • 举报
回复
受教了。
zjz800800 2004-10-22
  • 打赏
  • 举报
回复
这是构造函数,表示在CEx04aView::CEx04aView()构造的时候调用了CRect::CRect(int l, int t, int r, int b)来初始化m_rectEllipse。
这种方法是指当类中有别的对象时,对别的对象要进行构造初始化。
notepad--v3.4 windows Notepad--v3.4.0-plugin-Installer.exe 是win10下面的插件版安装包,会关联右键菜单等。 Notepad--v3.4.0-win10-portable.zip 是绿色免安装版本,解压即用,不会关联右键菜单注册表。 Ndd-quick-v3.3.0-win10-single-portable.zip 是单文件绿色免安装版,只包含皮肤和vc依赖库,不含插件、不含文件对比,主推轻量级、快速反应。适合只需要纯粹、轻快级,文本编辑器的用户。不定期发布。 MacOS 版本 Notepad--v3.4.0-mac_x64_12.3.dmg 是macos 12.x 及以后的版本。 Notepad--v3.4.0-mac_arm64_12.3.dmg 是macos 12.x 及以后 arm64 m1/m2芯片 的版本。第一次安装时,需要在设置偏好里面,放开苹果的安装限制,才能正常识别,请自行放开设置一下。 如果还是有问题,参考帖子:#I8JTJN:macOS Sonoma 14.1.1安装提示已损坏:macOS Sonoma 14.1.1安装提示已损坏 uos com.hmja.notepad_3.4.0.0_amd64.deb 是x64 cpu架构的uos系统对应的ndd版本。 其余系统版本后续会发布。 3.4 修改如下: 1 支持文件标签拖入拖出到新窗口的效果。 2 windows下修改快捷键放开。 3 按行号切分大文件。 4 大文件打开时,在文件夹查找所在目录,macos下可能会崩溃问题。 5 目录右键增加删除文件、文件夹功能。 6 补充深色主题下rust语法高亮; lisp 语法失效问题。 7 linux下信号打开文件,不拿锁,打开文件在消息队列中去做。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧