-2

Recently, I am learning MFC, following codes puzzled me a lot:

  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
...

The base class of CRect is a struct! I never learned this before.And if I call

CWnd::GetClientRect(LPRECT lpRect);

I can use rect or &rect (CRect rect)as the parameter.It's amazing!

I want to know some rules about the class with struct base. Thank you!

coqer
  • 307
  • 1
  • 9
  • @KirilKirov `RECT` is a `struct` in Win32 – bash.d Mar 26 '13 at 13:05
  • IIRC correctly this doesn't really have anything to do with a 'struct base'. If you look at `CRect` I think you'll see this method `operator LPRECT() { return this; }` which is the cast operator called to automatically convert a CRect to an LPRECT. And yes it's a neat trick, which works well almost all the time. – john Mar 26 '13 at 13:06

1 Answers1

5

In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.

C++ class Default Inheritance = private Default Access Level for Member Variables and Functions = private

C++ struct Default Inheritance = public Default Access Level for Member Variables and Functions = public

In short, yes, class can inherit from struct in C++.

Saqlain
  • 17,490
  • 4
  • 27
  • 33
  • OK,I know your meaning.But my question is why rect is equal to &rect? – coqer Mar 26 '13 at 13:29
  • Very simple have a look at http://msdn.microsoft.com/en-us/library/78y4kakb(v=vs.80).aspx, if you got any questions please do share... – Saqlain Mar 26 '13 at 17:22