Class design...

D

deancoo

I'm hoping this isn't too off topic, but I'm not sure if I've included some
of my member functions in the right class. I needed to develop numerous
functions to help define object A. These functions currently exist in the
class definition for object A. The thing is, there is another object (B)
who's job it is to call these functions when the app starts up, defining all
possibilities for object A's, then allow future object A's to look up its
attributes from object B without having to re-compute them all over again.
This is a big cost savings. Now realizing that the functions mentioned
above only get called by object B (once), shouldn't they be defined in
object B? Or is this one of those 'same difference' kind of things?

d
 
V

Victor Bazarov

deancoo said:
I'm hoping this isn't too off topic, but I'm not sure if I've included some
of my member functions in the right class. I needed to develop numerous
functions to help define object A. These functions currently exist in the
class definition for object A. The thing is, there is another object (B)
who's job it is to call these functions when the app starts up, defining all
possibilities for object A's, then allow future object A's to look up its
attributes from object B without having to re-compute them all over again.
This is a big cost savings. Now realizing that the functions mentioned
above only get called by object B (once), shouldn't they be defined in
object B? Or is this one of those 'same difference' kind of things?

It seems that the functions you're talking about are part of some kind of
initialisation process and really don't belong to any class, except, maybe
the one that uses them. Now, it would seem that some kind of A's static
initialiser is what you need. Without knowing more of your model, it is
rather impossible to be sure, but have you tried

class A {
class A_initialiser {
public:
A_initialiser() { // do all the stuff here
}
};

static A_initialiser a_init;
public:
// blah blah
};

A::A_initialiser A::a_init; // definition

Now, you could either rely on the initialisation of this static data
member of the class A (like above), or have your B do

class B {
// blah
void initialise_A() {
static A_initialiser a_init; // need to make that class
// A_initialiser public in A or make
// B a friend of A
}
};

which will allow you to control the time (or the event) of the A's class-
wide initialisation.

V
 
D

deancoo

Victor Bazarov said:
It seems that the functions you're talking about are part of some kind of
initialisation process and really don't belong to any class, except, maybe
the one that uses them. Now, it would seem that some kind of A's static
initialiser is what you need. Without knowing more of your model, it is
rather impossible to be sure, but have you tried

class A {
class A_initialiser {
public:
A_initialiser() { // do all the stuff here
}
};

static A_initialiser a_init;
public:
// blah blah
};

A::A_initialiser A::a_init; // definition

Now, you could either rely on the initialisation of this static data
member of the class A (like above), or have your B do

class B {
// blah
void initialise_A() {
static A_initialiser a_init; // need to make that class
// A_initialiser public in A or make
// B a friend of A
}
};

which will allow you to control the time (or the event) of the A's class-
wide initialisation.

V

Hmmm, interesting. So after you do a 'class wide' initialization, what
happens to a new instance of the class?
 
V

Victor Bazarov

deancoo said:
Hmmm, interesting. So after you do a 'class wide' initialization, what
happens to a new instance of the class?

Didn't you say that every instance of the class is supposed to use the
values resulted from the initialisation. So, every instance accesses
the values in 'a_init' and consumes them as needed.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top