[c++] include constructor?

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
I'm playing around with c++, and noticed constructors exist in classes.
However, is their a simpler way to execute a function automatically when you include a cpp \ h file?

I'm trying to make a server application, and just wanted to debug some results automatically.
This includes displaying defined constants in my file, but I don't really want to make a function call from main().

And I don't mean including the file from withing main(), I just want a simple private constructor or something called automatically.
 

Samael88

Evil always finds a way
Reaction score
181
Actually, I don't think that this you are talking about is possible in any programming language :S

The include part in c++ is only for the compiler and never becomes lines in the actual program, they are simply to instruct the program to include the data from those files.
 

GooS

Azrael
Reaction score
154
You want a constructor for something that is not an object?

anyway, why not make a functioncall from main?

And lastly, couldn't you make an instance of the server app object within main and nothing else?

your main function would only be like:

Code:
int main()
{
   ServerApp * bootApp = new ServerApp;
   delete bootApp;
   return 0;
}

And do whatever it is you want done in the constructor of the ServerApp object(class)?

//==GooS

PS: Also, I didn't really get the question.
 

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
What I'm talking about is simply that you can't execute code, that is not in a function. In other languages, it is possible possible to execute code on include. Since this gives me compile errors, I was thinking of that their was maybe a constructor.
I don't want to write a class with a constructor to print out debug messages.

For example:

-> constants.h
Code:
void myFunction(){

}

myFunction(); // <- Syntax error.

-> main.cpp
Code:
#include "constants.h"

int main(){
    // #include "consants.h" -> Possible, however.. I don't really like that idea.
    return 0;
}
 

GooS

Azrael
Reaction score
154
Ah now I get what you're getting at, that all things need to be within a defined scope, such as would not be required with lets say Python.

However you shouldn't think of the complete "document" as you application, your main function is your application.

In other languages this first scope has already been made for you but it's not visible, because it always needs to be there.

The include above the functions only links the abstracted resources to the application so the compiler knows what is to be used and where to find it.

No idea how correct this is, but it's how I think of it and it's never proven me wrong.

//==GooS
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top