Wrap entry point main() in C++

M

mosfet

Hi,

I would like to know how usually you wrapp an ugly main fonction into a
;-) C++ method.
I would like to write a simple console application that read a XML file
so it starts like this :


I was thniking about defining a MyApp class with a Init and Run methode:


class MyApp
{
public:

....
void Init(int argc, char* argc[]);
int Run();

};

int main(int argc, char* argv[])
{
MyApp myApp;

myApp.Init(argc, argv);
return myApp.Run();

}

Better idea ?
 
A

Alf P. Steinbach

* mosfet:
Hi,

I would like to know how usually you wrapp an ugly main fonction into a
;-) C++ method.

Don't.

If the function is ugly, the refactor based on whatever it is it does.

Don't decide in advance what that refactoring should end up with.

I would like to write a simple console application that read a XML file
so it starts like this :

With nothing?

I was thniking about defining a MyApp class with a Init and Run methode:


class MyApp
{
public:

...
void Init(int argc, char* argc[]);
int Run();

};

int main(int argc, char* argv[])
{
MyApp myApp;

myApp.Init(argc, argv);
return myApp.Run();

}

Better idea ?

Consult your C++ textbook about constructors.

Cheers, & hth.,

- Alf
 
E

Erik Wikström

Hi,

I would like to know how usually you wrapp an ugly main fonction into a
;-) C++ method.
I would like to write a simple console application that read a XML file
so it starts like this :


I was thniking about defining a MyApp class with a Init and Run methode:

Why would you, in main() you typically perform a number of actions that
have nothing to do with any of the objects, such as verifying that the
supplied arguments are valid, initialising the "environment", and so on.
Once that is done you can start creating the objects that and call their
methods. Notice thought that for some applications the objects are only
used to get a better structure and the flow of the program is driven by
stand alone functions. If you come from a Java background this might
sound a bit strange but that is just because Java lacks support for
normal functions, instead they use static member functions and such.
 

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
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top