Use Object in Header File Field

Hello all,

I need to use an instance of an object in a header file.
The object requires a lot of pre-computation to be created.

I want to assign it to a "static const *NAMEobject*" field in the .h file, once it has been created. I assume that it is better to create it in another file and somehow pass it into the .h file.

Any ideas would be greatly appreciated.
I don't understand your question exactly. Would you please post some code fragments describing your idea?
The usual way would be to define the object in an implementation file ande declaring it extern in a header.
Or do you want to define it as a contant class attribute?

What is exactly your problem?
Hello, yes. I just read about extern and it's possibly the solution.

The code is huge. It might be better to write pseudo code.
Basically, I want an object as a field for all class instances. These instances would be created and they would point to this object.
The object field should be created only once, since it will be the same for all instances. However, I am still without a clue how to create this object and pass it into a field to the header file.
In case it helps:
Code for .h file
1
2
3
4
5
6
7
class group	{
public:
	group();
	group(const group&);
	~group();
	
          const static field F;   // this would be the object created prior to execution of .h file. It requires                    //computation and passing of various parameters to be created.  


group.h

1
2
3
4
5
6
7
8
9
10
11
class group
{
public:
    group();
    group(const group &);
    ~group();

    static const field F;

    // ....
};


group.cpp

1
2
3
4
5
6
7
8
const field group::F; // Create F by default constructor

group::group()
{
    // ....
}

// .... 
Should do all you've expected.
It will require too much work to create the field with each instance. I am trying to create the field once and set it as a const field F, since it would be the same for all objects.
But your solution does create group::F exactly only once and before creating any group object.
An attribute marked static belongs the class - not any object.
Ok.
In order to create F, I need to call some methods and pass F as a parameter. How would I do this from group.cpp?

I get an uninitialized const error, when I just do:
const field group::F;

Thank you, tcs.
Last edited on
1. Sorry, but why the hell (another sorry ;-)) do you want to call some functions to create F? F will be created on process startup before main() will be called. Or did I misunderstood you? (BTW: methods can't be called. They will be activated by sending a message to an object. Only functions are callable).

2. Error report by gcc:
group.cpp:3:20: error: default initialization of an object of const type 'const field' requires a user-provided default constructor
const field group::F; // Create F by default constructor

So provide a default constructor for class field.
And another hint: Surely you may omit const from F if you want to modify F later in your program.
I want to call some functions to create F.
I am calling in my no-arg constructor the function to initialize F.
1
2
3
4
5
6
7
{
// code inside constructor

init_ab( group::F, ...);

}


Is this a correct usage? The code throws no compilation error. Notice that I wrote group::F inside the constructor and not outside all Functions or methods as I have seen in other code.

I usually see it as:
1
2
3
4
5
group::F= ...//something

methodFunciton(){}
methodFUnction2(){}
If the no-arg constructor is your field class default constructor then its not the best idea to access group::F inside it because F is just of type field. In simple words: F is just under construction.

Constructing a class attribute (C++ "static member variable") from inside an objects constructor does make sense only for singletons and will not work on readonly class attributes (those one marked const). Sometimes its useful to incarnate a class to an object only once during process lifetime. F.e. an object containing and manageing user defaults like font sizes and so on. This will be called a singleton

You may initialze group::F f.e. from a function f() as

const field group::F = f();

in your group.cpp file where f() has to return a field object. f() may be any C++ function or any class method (a C++ "static member function").


methodFunciton(){}
BTW: Even if they look like functions, methods aren't functions. Functions exists only once and aren't bound to any object while methods conceptionally exists multiple times, one per object.
Thank you.
Yes, I want to create a singleton. I didn't know the name for it.
The problem with initializing group::F is that the function to initialize F requires F as a parameter. I am not sure, if it's a problem at all.

The function is as follows:
curve_init_ab(F, a, b, mod)

It will use a,b, and mod to initialize F.

I have called this function inside the main constructor in order to make the code compile. However, I am not sure, if it's of correct usage.

1
2
3
4
5
{
//inside constructor
curve_init_ab(group::F, a, b, mod)   //notice how I called it, using F after ::   , It gave me no errors.

}


If the first argument of curve_init_ab() is of type field or field& the compiler won't complain any error. When passing line 3 the compiler doesn't know whether F is just initialized or not.

To initialize F this way the first argument of curve_init_ab should be of type field&.

But I suggest the following way:

group.h

#include "field.h"

1
2
3
4
5
6
7
8
9
10
11
12
13
class group
{
private:
    static field F_initializer();

public:
    static const field F;

public:
    group();
    // ...

} /* group */;



group.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "group.h"

const field group::F = group::F_initializer();

field group::F_initializer()
{
    field initialF;                      // Create a new `fieldĀ“ object...
    // Now you've a completely initialized `fieldĀ“ which can be used as argument to any further processing

    curve_init_ab(initialF, a, b, mod);  // ... initialize it as appropriate...

    return initialF;                     // ... and return it to be copied to the final F

} /* F_initializer() */

// ... 


Doing this way
1. your C++ runtime system guarantees F having been initialized before any group object will be constructed, and
2. curve_init_ab() will be applied to a completely initialized field object.

Surely you may add an argument list to group::F_initializer() if needed.
Thank you. You have been of great help.
I will follow your advice; however, I won't mark this thread as solved until I have tested it thoroughly.

Thank you, again.

Topic archived. No new replies allowed.