Thread: Why are using declarations passed over but not includes

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Why are using declarations passed over but not includes

    Let's say in a header file I use a using declaration for namespace or a certain class, if any file includes that header they all share the using declaration, but what about the includes. I have to use them no matter if the header file has the include declaration.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You could create one header file that holds all the #include files you need in the project., along with any using declarations. Then, when you need to use the files, just include that one header file. I call mine "sharing.h"
    Double Helix STL

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You must be doing something wrong, indigo.

    If B includes A, and C includes B. C will also have A included.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    So when I'm doing an implementation file of a header, I don't have to repeat the #include <someclass> header in the implementation. I always thought it was the case that you had to.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... but that's different. The implementation goes in a cpp file. This file must have the header file included in order for all those declarations to come into scope.

    Code:
    // Header file - > SomeClass.hpp
    
    class SomeClass {
        public:
            int sum_members();
        private:
            int a;
            int b;
    };
    Code:
    // implementation file - > SomeClass.cpp
    
    #include "SomeClass.hpp"
    
    int SomeClass::sum_members() {
        return a + b;
    }
    Now your class is fully defined and implemented.

    In main you only need to include SomeClass.hpp if you want to create SomeClass objects.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I know all that, I meant something like this


    Code:
    // Header file - > SomeClass.hpp
    
    #include<vector>
    
    class SomeClass {
        public:
            int sum_members();
        private:
            vector a;
            int b;
    };
    Code:
    // implementation file - > SomeClass.cpp
    
    #include "SomeClass.hpp"
    #include <vector>
    
    int SomeClass::sum_members() {
        return a + b;
    }

    I thought it was the case that you had to #include <vector> in both the implementation and header, unlike the using declarations which cross over once the header file is included in the implementation file.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Are you talking about this ?

    Header file - > SomeClass.hpp
    Code:
    #include<vector>  // that is not really needed if you include it in the implementation file
    
    // headers should not have any using decl.
    
    class SomeClass {
        public:
            int sum_members();
        private:
            std::vector<int> a;  // fully qualified
            int b;
    };
    implementation file - > SomeClass.cpp

    Code:
    #include "SomeClass.hpp"
    #include <vector>
    using std::vector;
    
    int SomeClass::sum_members() {
        vector<int>::iterator i = a.begin();
        return *i + b;
    }

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > I thought it was the case that you had to #include <vector> in both the implementation and header, unlike the using declarations which cross over once the header file is included in the implementation file.

    Yeah. You don't need the include on the cpp file, once it has been done on the header file.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 02:03 PM
  2. Makefile and includes
    By Lang in forum C Programming
    Replies: 4
    Last Post: 03-18-2008, 03:29 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Manipulating a passed pointer inside a function.
    By see_c in forum C Programming
    Replies: 12
    Last Post: 05-25-2006, 02:35 PM
  5. #includes
    By RedLenses in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2002, 03:59 PM