Question

If I have some code like

    main(int argc, char *argv[])
{
...
#include "Class1.H"
#include "Class2.H"
...

}

Generally the main() method is the starting point of every application and the content within main() is to be executed. Am I right in the assumption that the content of all classes included into main() will be executed when main() is started?

greetings Streight

Was it helpful?

Solution

No, no, NO.

First of all, you don't #include a file within a function. You #include a file at the beginning of a file, before other declarations. OK, you can use #include anywhere, but you really just shouldn't.

Second, #include doesn't execute anything. It's basically just a copy-paste operation. The contents of the #included file are (effectively) inserted exactly where you put the #include.

Third, if you're going to learn to program in C++, please consider picking up one of our recommended texts.


You commented:

I am working with the multiphaseEulerFoam Solver in OpenFoam and inside the main() of multiphaseEulerFoam.C are classes included. I assume that the classes have the right structure to be called in main()

That may be the case, and I don't doubt that the classes have the right structure to be called from main. The problem is main will be malformed after the #includes because you'll have local class definitions and who knows what else within main.

Consider this. If you have a header:

foo.h

#ifndef FOO_H
#define FOO_H

class Foo
{
public:
  Foo (const std::string& val)
  :
    mVal (val)
  {
  }
private:
  std::string mVal;
};

#endif

And you try to include this in main:

main.cpp

int main()
{
  #include "foo.h"
}

After preprocessing the #include directive, the resulting file that the compiler will try to compile will look like this:

preprocessed main.cpp

int main()
{
    #ifndef FOO_H
    #define FOO_H

    class Foo
    {
    public:
      Foo (const std::string& val)
      :
        mVal (val)
      {
      }
    private:
      std::string mVal;
    };

    #endif
}

This is all kinds of wrong. One, you can't declare local classes like this. Two, Foo won't be "executed", as you seem to assume.

main.cpp should look like this instead:

#include "foo.h"

int main()
{
}

OTHER TIPS

#define and #include are just textual operations that take place during the 'preprocessing' phase of compilation, which is technically an optional phase. So you can mix and match them in all sorts of ways and as long as your preprocessor syntax is correct it will work.

However if you do redefine macros with #undef your code will be hard to follow because the same text could have different meanings in different places in the code.

For custom types typedef is much preferred where possible because you can still benefit from the type checking mechanism of the compiler and it is less error-prone because it is much less likely than #define macros to have unexpected side-effects on surrounding code.

Jim Blacklers Answer @ #include inside the main () function

Try to avoid code like this. #include directive inserts contents of the file in its place. You can simulate the result of your code by copy-pasting file content from Class1.H and Class2.H inside the main function.

Includes do not belong into any function or class method body, this is not a good idea to do. No code will be executed unless you instantiate one of your classes in your header files.

Code is executed when:

  1. Class is instantiated, then it's constructor method is called and the code inside the method is executed.
  2. If there are variables of a class type inside your instantiated class, they will too run their constructors.
  3. When you call a class method.

Try this example:

#include <iostream>
using namespace std;

int main()
{
    class A
    { public:
        A() { cout << "A constructor called" << endl; }
    };
    // A has no instances
    class B
    { public:
        B() { cout << "B constructor called" << endl; }
        void test() { cout << "B test called" << endl; }
    } bbb;
    // bbb will be new class instance of B
    bbb.test(); // example call of test method of bbb instance
    B ccc;      // another class instance of B
    ccc.test(); // another call, this time of ccc instance
}

When you run it, you'll observe that:

  1. there will be no instance of class A created. Nothing will be run from class A.
  2. if you intantiate bbb and ccc, their constructors will be run. To run any other code you must first make a method, for example test and then call it.

This is an openFoam syntax he is correct in saying that open Foam treats #include like calling a function. In OpenFoam using #include Foo.H would run through the code not the class declaration that is done in a different hierarchy level. I would recommend all openFoam related question not be asked in a C++ forum because there is so much stuff built onto C++ in openFoam a lot the rules need to be broken to produce a working code.

You're only including declarations of classes. To execute their code, you need to create class instances (objects).

Also, you shouldn't write #include inside a function or a class method. More often than not it won't compile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top