 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Per Bull Holmen Guest
|
Posted: Mon Dec 25, 2006 8:50 am Post subject: static class initialization functions in c++? |
|
|
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it. Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
before the first instance of that particular class is created. It is a
static member function, which otherwise operates the same way as any
other such functions: It can be overridden by subclasses, and the
overridden can call the superclass' implementation if needed, etc...
I have been looking, but not found an equivalent in c++. Is there one?
Per |
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Mon Dec 25, 2006 9:19 am Post subject: Re: static class initialization functions in c++? |
|
|
"Per Bull Holmen" <pbholmen (AT) yahoo (DOT) com> wrote in message
news:1hqvu7r.1l70icp1rb3qhmN%pbholmen (AT) yahoo (DOT) com...
| Quote: | Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it.
|
In C++, only a object can be initialized. A class is not an object.
| Quote: | Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
before the first instance of that particular class is created.
|
A C++ class can contain static member objects, which are initialized
before 'main()' begins. If there is more than one static (member or
nonmember) object in the program, the order of their initialization
is not specified by the language.
| Quote: | It is a
static member function,
|
In C++, only one member function of a class can initialize that
class's data members: the constructor.
| Quote: | which otherwise operates the same way as any
other such functions: It can be overridden by subclasses, and the
overridden can call the superclass' implementation if needed, etc...
|
A constructor can not be overridden. Creation of an object of a derived
type will automatically cause its base class' constructor to be invoked.
| Quote: | I have been looking, but not found an equivalent in c++. Is there one?
|
class B
{
static int i;
int j;
public:
B(int arg = 0) : j(arg) /* constructor */
{
}
};
int B::i = 42; /* initialize B::i with the value 42 */
class D : public B
{
static int m;
int k;
public:
D(int Bval, int kval) : B(Bval), k(kval) /* constructor */
{
}
};
int D::m = 10; /* initialize D::m with the value 10 */
int main()
{
B b_obj(99); /* create an object of type 'B', and
intialize it's member 'j' with the
value 99 */
D d_obj(25, 32); /* create an object of type 'D', initialize
its base portion (via its constructor 'B::B()')
with value 25, and initialize its member 'k' with
value 32 */
return 0;
}
If you tell us more specifically what you're trying to do,
perhaps we can offer more specific advice.
Which C++ book(s) are you reading?
-Mike |
|
| Back to top |
|
 |
Per Bull Holmen Guest
|
Posted: Mon Dec 25, 2006 10:10 am Post subject: Re: static class initialization functions in c++? |
|
|
Mike Wahler <mkwahler (AT) mkwahler (DOT) net> wrote:
| Quote: | If you tell us more specifically what you're trying to do,
perhaps we can offer more specific advice.
|
I'm trying to write a base class for scriptable objects. The subclasses
of this class hold a set of properties that must be identified by name
(a string), that can be retrieved or set by a script. But never mind the
scripting, I just explain it so you can see why the properties must be
identified by names. When the member functions
int Scriptable::luaGetProperty( lua_State *lua, string &propertyName )
int Scriptable::luaSetProperty( lua_State *lua, string &propertyName )
are called on the scriptable objects, the respective subclasses must be
free to implement the getting and setting of these properties
themselves, so it's not possible to just let these properties be held by
some hashtable. It would be nice to let the subclass just forward the
requests to the following member functions:
int Scriptable::luaGetProperty( lua_State *lua, int propertyCode )
int Scriptable::luaSetProperty( lua_State *lua, int propertyCode )
which take an int code (i.e. an enum) instead of an action name, cause
then I can just use switch statements like this:
switch( propertyCode ) {
case HEIGHT_PROPERTY:
// perform code to get/set height
case WIDTH_PROPERTY:
// etc...
}
My problem is the following: I thought it would be a bad idea to
hardcode all property names and respective codes in the base class. So,
I thought I'd let the base class hold a registry of property names and
codes in a hashtable, as a static member shared by subclasses. Then, any
subclass that adds new properties, can call the function:
void Scriptable::registerPropertyCode( string &name, int code ) {
properties.put( name, code );
}
Where properties is the hashtable. In the main other language I've been
programming in (Objective-C), I would simply make the class initializer
call a static member function, declared something like this:
class Scriptable {
/.../
static void registerProperties();
/.../
};
So that any subclass can override this to add their own properties. I
thought the cleanest way to do this, would be to make the class
guarantee that the properties were registered before any instances of
the class were set into action. I might do it in the constructors,
though, but I thought it wouldn't be optimal. I'm sure there's a
workaround for this specific problem, but I'm trying to learn how to
solve this type of problems in the future
| Quote: | Which C++ book(s) are you reading?
|
"Core C++ A Software Engineering Approach" by Victor Shtern.
Per |
|
| Back to top |
|
 |
Per Bull Holmen Guest
|
Posted: Mon Dec 25, 2006 10:10 am Post subject: Re: static class initialization functions in c++? |
|
|
Mike Wahler <mkwahler (AT) mkwahler (DOT) net> wrote:
| Quote: | If you tell us more specifically what you're trying to do,
perhaps we can offer more specific advice.
|
I'm trying to write a base class for scriptable objects. The subclasses
of this class hold a set of properties that must be identified by name
(a string), that can be retrieved or set by a script. But never mind the
scripting, I just explain it so you can see why the properties must be
identified by names. When the member functions
int Scriptable::luaGetProperty( lua_State *lua, string &propertyName )
int Scriptable::luaSetProperty( lua_State *lua, string &propertyName )
are called on the scriptable objects, the respective subclasses must be
free to implement the getting and setting of these properties
themselves, so it's not possible to just let these properties be held by
some hashtable. It would be nice to let the base class just forward the
requests to the following member functions:
int Scriptable::luaGetProperty( lua_State *lua, int propertyCode )
int Scriptable::luaSetProperty( lua_State *lua, int propertyCode )
which take an int code (i.e. an enum) instead of an action name, cause
then I can just use switch statements like this:
switch( propertyCode ) {
case HEIGHT_PROPERTY:
// perform code to get/set height
case WIDTH_PROPERTY:
// etc...
}
My problem is the following: I thought it would be a bad idea to
hardcode all property names and respective codes in the base class. So,
I thought I'd let the base class hold a registry of property names and
codes in a hashtable, as a static member shared by subclasses. Then, any
subclass that adds new properties, can call the function:
void Scriptable::registerPropertyCode( string &name, int code ) {
properties.put( name, code );
}
Where properties is the hashtable. In the main other language I've been
programming in (Objective-C), I would simply make the class initializer
call a static member function, declared something like this:
class Scriptable {
/.../
static void registerProperties();
/.../
};
So that any subclass can override this to add their own properties. I
thought the cleanest way to do this, would be to make the class
guarantee that the properties were registered before any instances of
the class were set into action. I might do it in the constructors,
though, but I thought it wouldn't be optimal. I'm sure there's a
workaround for this specific problem, but I'm trying to learn how to
solve this type of problems in the future
| Quote: | Which C++ book(s) are you reading?
|
"Core C++ A Software Engineering Approach" by Victor Shtern.
Per |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|