【问题标题】:pointer or variable? [closed]指针还是变量? [关闭]
【发布时间】:2013-03-16 06:57:30
【问题描述】:

最近在学习MFC,下面的代码让我很困惑:

  class CRect : public tagRECT
{
public:

// Constructors

// uninitialized rectangle
CRect();
// from left, top, right, and bottom
CRect(int l, int t, int r, int b);
// copy constructor
CRect(const RECT& srcRect);
// from a pointer to another rect
CRect(LPCRECT lpSrcRect);
// from a point and size
CRect(POINT point, SIZE size);
// from two points
CRect(POINT topLeft, POINT bottomR
...

CRect 的基类是一个结构体!我以前从来没有学过这个。如果我打电话给

CWnd::GetClientRect(LPRECT lpRect);

我可以使用rect&rect(CRect rect)作为参数,太棒了!

我想知道一些关于 struct base 类的规则。谢谢!

【问题讨论】:

  • @KirilKirov RECT 是 Win32 中的 struct
  • IIRC 正确,这与“结构基础”没有任何关系。如果您查看CRect,我想您会看到此方法operator LPRECT() { return this; },它是调用转换运算符以自动将CRect 转换为LPRECT。是的,这是一个巧妙的技巧,几乎一直有效。

标签: c++ pointers struct base-class


【解决方案1】:

在 C++ 中,类和结构是相同的,除了它们在继承和成员访问级别方面的默认行为。

C++ 类 默认继承 = 私有 成员变量和函数的默认访问级别 = 私有

C++ 结构体 默认继承 = 公共 成员变量和函数的默认访问级别 = public

简而言之,是的,类可以从 C++ 中的 struct 继承。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多