Can you name the class that this public constructor belongs too? Are we to assume that the protected one is class A? |
There is a super-class named TObject:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
class TObject {
friend class TBasis;
friend class TComponent;
private:
unsigned _typeIndex;
bool _passFlag;
TObject* _prevObject;
TObject* _nextObject;
TObject* _baseObject;
TObject* _subObject;
void loopUpdate();
void loopDelete();
virtual void update() = 0;
unsigned _orderIndex; //little optimization rudiment
protected:
TObject(unsigned typeIndex, bool passFlag);
public:
unsigned const& typeIndex = _typeIndex;
bool const& passFlag = _passFlag;
TObject* const& prevObject = _prevObject;
TObject* const& nextObject = _nextObject;
TObject* const& baseObject = _baseObject;
TObject* const& subObject = _subObject;
virtual ~TObject();
virtual void remove() = 0;
};
|
There are also 2 basic classes inheriting Object: Basis and Component.
Basis:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class TBasis : public TObject {
friend class TObject;
friend class TComponent;
private:
unsigned _orderSize;
TComponent* * orderComponent;
TObject* getPosition(unsigned orderIndex);
virtual void update() = 0;
protected:
TBasis(int dummy, unsigned typeIndex, TObject* baseObject, unsigned orderSize);
public:
unsigned const& orderSize = _orderSize;
virtual ~TBasis();
void updateAll();
void remove();
};
|
Component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
class TComponent : public TObject {
friend class TObject;
friend class TBasis;
private:
//unsigned _orderIndex;
virtual void update() = 0;
protected:
TComponent(int dummy, unsigned typeIndex, TObject* baseObject/*!= 0*/, unsigned orderIndex);
public:
unsigned const& orderIndex = _orderIndex;
virtual ~TComponent();
void remove();
};
|
Object, Basis, Component are all abstract and have only protected constructor.
Let's now take, for example, TSprite::TComponent:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class TSprite : public TComponent {
private:
spriteset_t* _spriteset;
int _x;
int _y;
unsigned column;
unsigned row;
float scale;
bool mirrorX;
bool mirrorY;
virtual void update();
protected:
TSprite(IM_ARGS(ARGS));
public:
spriteset_t* const& spriteset = _spriteset;
int const& x = _x;
int const& y = _y;
TSprite(DEFS);
void setSpriteset(spriteset_t* spriteset);
void setCoords(int x, int y);
};
|
It has a protected constructor and a public constructor.
So, every class that is not basic (not just Object, Basis or Component) has also public constructor.
Public constructor is for user's purpose to create objects like this:
1 2 3 4 5 6
|
Frame = new TFrame();
new TCursor(Frame);
World = new TWorld(new TBackground(Frame));
World->setTilebox(new TTilebox(World))
new TAllroundScroller(World);
|
Protected one is to "inherit" (sorry for this, how will it be correct to say?) basic class or another non-basic like this:
1 2 3
|
inline TSprite::TSprite(IM_ARGS(ARGS)) : TComponent(IM_VALS(CCOMPONENT_VALS)) {
///some code
}
|
Then public one is empty, as it "inherits" protected (to avoid much repetitive code):
1 2
|
inline TSprite::TSprite(ARGS) : TSprite(IM_VALS(VALS)) {
}
|
All this is done to make sure that user does not modify typeIndex by himself.
(Protected has typeIndex, public - doesn't)
As for using a struct instead of a macro for the parameters, there are pros and cons for both. I prefer the macro solution. Both solutions still type check. Both solutions require searching for their definition if you do not know what they mean already. The struct enforces something that I like in python: named parameter passing. The macro provides less typing in the end, and does not require the user to have local struct variables just to pass a set of parameters. |
I wasn't even sure if people ever use it for such purpose.
Ty :]
Are you trying to have a unique identifier for every class you initialize that is a descendant of the oldest ancestor? You could just have a static integer be incremented in the oldest ancestor's constructor. |
Yep.
I use boost::preprocessor to make unique indexes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
# ifndef KENG_MAIN_TYPE_INDEX_HPP_INCLUDED
# define KENG_MAIN_TYPE_INDEX_HPP_INCLUDED
# include <boost/preprocessor/arithmetic/inc.hpp>
# include <boost/preprocessor/slot/slot.hpp>
# define BOOST_PP_VALUE 0
# include BOOST_PP_ASSIGN_SLOT(1)
# undef BOOST_PP_VALUE
# define UNIQUE_OBJ_TYPE_INDEX BOOST_PP_SLOT(1)
# define OBJ_TYPE_COUNT BOOST_PP_SLOT(1)
# else
# define BOOST_PP_VALUE BOOST_PP_INC(BOOST_PP_SLOT(1))
# include BOOST_PP_ASSIGN_SLOT(1)
# undef BOOST_PP_VALUE
# endif
|
If the class is named TClass, then TCLASS_INDEX is its index.
I just need to include this header in every class header.