Results 1 to 9 of 9

Thread: All my code in one (cpp) file?

  1. #1

    Thread Starter
    Fanatic Member Kings's Avatar
    Join Date
    Aug 2001
    Posts
    673

    All my code in one (cpp) file?

    Am I supposed to put all my code in one cpp file, or can I spread them out over several files?

    If I can spread them out, mind telling me how? I'm using VC++.
    K i n g s

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Just create new C++ files, and use the extern keyword to reference things in other source files, ex.
    Code:
    extern int myfunc(int, int, long int);
    ... I think. I'm really not all that good with C++.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You don't even need the extern keyword.

    Just add new .cpp files to the project and make sure you have function prototypes when you need them and no name conflicts.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member Kings's Avatar
    Join Date
    Aug 2001
    Posts
    673
    Cool, thanks
    K i n g s

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Wow, then I must've read my book wrong, because it says you always have to use it. When do you use it, then? Only for variables?
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes. Functions are extern by default, as are variables, but you need to the extern keyword for variables to distinguish between a declaration and a definition:
    Code:
    // function declaration: no code block
    // is extern by default
    // there may be as many as you want that
    // look the same as each one is only a hint
    // for the compiler
    void func(void);
    
    // function definition: has a code block
    // is extern by default
    // only one in the whole code, else the linker
    // won't be able to decide which one to use
    void func(void)
    {
      exit(0);
    }
    
    // variables don't have code blocks
    // therefore this is a definition
    // extern linkage by default
    // only one allowed in the whole code
    int global_integer;
    
    // to get a pure declaration you have to
    // add the extern keyword
    // all variables declared in header files
    // need this keyword
    extern int global_integer;
    The alternative is to mark things as static, but this has a very different meaning. Don't do it unless you know what it means.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I believe I do know what static means (even after the scope it's in has left, it is still initialised and has all the data in it?)
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    static has 3 different meanings in C++, what you posted is just one of them.
    I'll be back and post something about it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The 3 meanings of static
    This is a prepared post ready for reposting and contains no contextual references to any thread.

    Meaning 1:
    Code:
    void count_calls(void)
    {
    	static int calls = 0;
    	calls++;
    }
    calls is a static local variable: it has the lifetime of a global variable and is initialized only once but can only be accessed from within this function.

    Meaning 2:
    Code:
    onefile.cpp
    static int some_variable;
    static void some_function(void)
    {
      // do something
    }
    
    anotherfile.cpp
    static int some_variable;
    static void some_function(void)
    {
      // do something
    }
    Defining two variables or functions with the same name in two different source files would normally lead to linker errors: references to the names cannot be properly resolved because of ambiguity. The static here means that the identifier has internal linkage, which means that the linker will not try to resolve references from other source files to this identifier. This means that the name can only be used within this file, but it also means that other files may use static identifiers with the same name.

    Meaning 3 (C++ only):
    Code:
    class myclass
    {
    	static int num_instances;
    
    public:
    	static int get_num_instances() {
    		return num_instances;
    	}
    
    	myclass() {
    		++num_instances;
    	}
    
    	~myclass() {
    		--num_instances;
    	}
    };
    
    int myclass::num_instances = 0;
    Here the members marked static are class members instead of instance members. You don't need an instance of the class to access them and they exist only once. They are shared between all instances of the class. Access from outside is usually done with the normal scope resolution operator:
    int inst = myclass::get_num_instances();
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width