C++ class pointer initialisation

Nov 7, 2008 at 12:47am
I'm an experienced C and Java programmer already, but I am using C++ for the first time. I am constantly encountering a problem which seems very simple, but I have yet to find an answer elsewhere!

My problem is that a line such as this:

MyClass classvar;

will automatically create a new instance of MyClass, which is different from what I am used to with Java, but manageable. However, this becomes a problem if MyClass does not expose a constructor which requires no arguments (ie MyClass()), as the arguments have to be provided the moment the variable initialises. This is an issue since I am unable to declare a global variable at the top of the source file and initialise it later on when the relevant argument values have also been initialised. Is there any way around this?

I feel like I must be missing something very obvious, so if anyone can help I would be very grateful!

Thanks
Nov 7, 2008 at 1:00am
> This an issue since I am unable to declare a global variable at the top of the source file and initialise it later on when the relevant argument values have also been initialised.

Why are you unable to? Does your religion prohibits such an action?
Nov 7, 2008 at 1:31am
What he means is that he wants to run the constructor for a class, but he doesn't have the data for the constructor until he is already into the main loop. To solve your problem:

Declare a pointer to MyClass. Then once you get the data, use new to create an instance of MyClass for the pointer. Remember to use delete at the end.

Ex:

1
2
3
4
5
6
7
8
9
MyClass* classvar;

int main() {
   //get data
   classvar = new MyClass(/*args*/);
   //do stuff with it
   delete classvar;
   return 0;
}


EDIT: I win :D
Last edited on Nov 7, 2008 at 1:34am
Nov 7, 2008 at 1:31am
Declare a global variable that is a pointer to the class you want. Then, when you get the parameters use new ClassName(/*constructor*/); to initialize it. Just make sure to delete it when you are finished.
Nov 7, 2008 at 2:39am
firedraco, Zhuge, your solution is good. I'll give mine.
He can create an object with default constructor and initialize it further.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class HisClass
{
public:
   HisClass() { m_initialized = false; }
   HisClass(/*variables list*/) { Init(/*variables list*/); }
   void Init(/*variables list*/) {/* initialization*/; m_initialized = true; }
private:
   m_initialized;
   .........
   .........
};

HisClass obj;

int main(){
   /*get needed variable list*/
   obj.Init(/*variables list*/);


He can do necessary verification as soon as needed:
if (obj.isInitialized() == false)
cerr << "Error: object is not initialized yet!";
Nov 7, 2008 at 2:36pm
Hi everyone, thanks for all your replies. Yes, it was me just being an idiot, it was because I had forgotten to declare it as a pointer and so g++ was trying to initialise it for me. Thats what you get for doing Java for 2 years and then jumping straight into C++!

Thanks again
Topic archived. No new replies allowed.