I wanted to make sure I still remembered all the ins and outs of inheritance, so I tried to break my compiler doing some wacky things with it.
I found that MSVC will not notice a protected constructor if you have the function body in the header. Put it in a .cpp, you're fine. MSVC sometimes doesn't notice you've made a change to implimentation in a header file, and requires you to clean and rebuild in order to have the change have any effect. This was not the case in this scenario.
I also found this weird break: if you declare a constructor or an assigment operator as protected, it won't let derived classes use them.
BaseProtected.h
Code:
class BaseProtected
{
protected:
BaseProtected();
const BaseProtected & operator = (const BaseProtected & b);
int prot;
};
BaseProtected.cpp
Code:
#include "BaseProtected.h"
BaseProtected::BaseProtected()
: prot(1)
{}
const BaseProtected & BaseProtected::operator = (const BaseProtected & b)
{
prot = b.prot;
return *this;
}
DerivedProtected.h
Code:
#include "BaseProtected.h"
class DerivedProtected : public BaseProtected
{
public:
DerivedProtected();
void DoStuff();
int newGuy;
};
DerivedProtected.cpp
Code:
#include "DerivedProtected.h"
DerivedProtected::DerivedProtected()
:newGuy(99)
{}
void DerivedProtected::DoStuff()
{
BaseProtected a, b; //MSVC error C2248
a = b;//MSVC error C2248
}
I find that interesting. If you were to try and mess with the prot member after a and d are declared, it also issues errors saying you can't access protected memebers. As far as I know, this is not exptected behavior.
If anybody else would like to mess around with this in MSVC or any other compiler, please let me know what results you get.
If you'd like to enlighten me, I'm up for that, too.