Type visibility

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • saneman

    #1

    Type visibility

    I have made two modules:

    1) main.cpp
    Contains a main function and struct P.
    Includes head.h and std::vector

    #include "head.h"
    #include<vector >
    #include<iostre am>
    struct P {
    int x;
    int y;
    };

    int main() {
    typedef std::vector<Pco ntainer;
    container v;
    P p;
    p.x = 22;
    p.y = 33;
    v.push_back(p);


    A<containera(v) ;
    int res = a.getValue();
    std::cout << res << std::endl;
    return 0;
    }




    2) head.h
    A header file that contains the class A.

    template <typename T >
    class A {
    public:
    T t;

    A(T t_){
    t = t_;
    }
    int getValue() {
    int res = (*t.begin()).x;
    return res;

    }
    };




    In class A getValue() returns the x field of the struct defined in main.cpp.
    But how does class A know about the type 'P' which is only created in main?


  • =?utf-8?Q?David_C=C3=B4me?=

    #2
    Re: Type visibility

    On Mon, 04 Aug 2008 12:32:28 +0200, saneman <asdff@asd.comw rote:
    I have made two modules:
    >
    1) main.cpp
    Contains a main function and struct P.
    Includes head.h and std::vector
    >
    #include "head.h"
    #include<vector >
    #include<iostre am>
    struct P {
    int x;
    int y;
    };
    >
    int main() {
    typedef std::vector<Pco ntainer;
    container v;
    P p;
    p.x = 22;
    p.y = 33;
    v.push_back(p);
    >
    >
    A<containera(v) ;
    int res = a.getValue();
    std::cout << res << std::endl;
    return 0;
    }
    >
    >
    >
    >
    2) head.h
    A header file that contains the class A.
    >
    template <typename T >
    class A {
    public:
    T t;
    >
    A(T t_){
    t = t_;
    }
    int getValue() {
    int res = (*t.begin()).x;
    return res;
    >
    }
    };
    >
    >
    >
    >
    In class A getValue() returns the x field of the struct defined in
    main.cpp.
    But how does class A know about the type 'P' which is only created in
    main?
    >
    >
    A doesn't know P. He knows just T must have a public member named x.
    Replace typedef std::vector<Pco ntainer; by
    typedef std::list<Qcont ainer; with struct Q {int x;} and it will works
    fine.

    Comment

    • Pascal J. Bourguignon

      #3
      Re: Type visibility

      "saneman" <asdff@asd.comw rites:
      I have made two modules:
      Good.

      1) main.cpp
      2) head.h
      Wrong.


      If you have two modules, you should have five files:

      interface implementation
      Module A: ModuleA.h ModuleA.cpp
      Module B: ModuleB.h ModuleB.cpp
      Main: main.cpp

      Assuming ModuleB uses ModuleA, and main uses both modules, you will have:

      ------(ModuleA.h)-----------------------
      #ifndef ModuleA_h
      #define ModuleA_h

      // public definitions of module A

      #endif
      ----------------------------------------


      ------(ModuleA.cpp)---------------------
      #include <ModuleA.h>

      // implementation of module A

      ----------------------------------------



      ------(ModuleB.h)-----------------------
      #ifndef ModuleB_h
      #define ModuleB_h

      #include <ModuleA.h>

      // public definitions of module B

      #endif
      ----------------------------------------


      ------(ModuleB.cpp)---------------------
      #include <ModuleB.h>

      // implementation of module B

      ----------------------------------------


      ------(main.cpp)------------------------
      #include <ModuleA.h>
      #include <ModuleB.h>

      // implementation of main:
      int main(int argc,char** argv,char** envv){
      ModuleA::doSome thing();
      ModuleB::doSome thing();
      return(0);
      }

      ----------------------------------------



      Note that you cannot have cross dependencies between the interfaces of
      the modules, but you may have cross dependencies between
      implementations and interfaces:

      ModuleC.cpp could include ModuleD.h
      and ModuleD.cpp could include ModuleC.h

      this would mean that both modules C and D are at the same layer.

      Otherwise the dependencies between the modules correspond to the
      layers structuring the software.



      --
      __Pascal Bourguignon__

      Comment

      • saneman

        #4
        Re: Type visibility


        "David Côme" <davidcome@wana doo.frskrev i en meddelelse
        news:op.ufczopz 9rttu86@debian. ..
        On Mon, 04 Aug 2008 12:32:28 +0200, saneman <asdff@asd.comw rote:
        >
        >I have made two modules:
        >>
        >1) main.cpp
        > Contains a main function and struct P.
        > Includes head.h and std::vector
        >>
        >#include "head.h"
        >#include<vecto r>
        >#include<iostr eam>
        >struct P {
        > int x;
        > int y;
        >};
        >>
        >int main() {
        > typedef std::vector<Pco ntainer;
        > container v;
        > P p;
        > p.x = 22;
        > p.y = 33;
        > v.push_back(p);
        >>
        >>
        > A<containera(v) ;
        > int res = a.getValue();
        > std::cout << res << std::endl;
        > return 0;
        >}
        >>
        >>
        >>
        >>
        >2) head.h
        > A header file that contains the class A.
        >>
        >template <typename T >
        >class A {
        > public:
        > T t;
        >>
        > A(T t_){
        > t = t_;
        > }
        > int getValue() {
        > int res = (*t.begin()).x;
        > return res;
        >>
        > }
        >};
        >>
        >>
        >>
        >>
        >In class A getValue() returns the x field of the struct defined in
        >main.cpp.
        >But how does class A know about the type 'P' which is only created in
        >main?
        >>
        >>
        >
        A doesn't know P. He knows just T must have a public member named x.
        Replace typedef std::vector<Pco ntainer; by
        typedef std::list<Qcont ainer; with struct Q {int x;} and it will works
        fine.
        But if the struct contains more fields the only way to make it work is using
        templates right?


        Comment

        • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

          #5
          Re: Type visibility

          On 2008-08-04 12:32, saneman wrote:
          I have made two modules:
          >
          1) main.cpp
          Contains a main function and struct P.
          Includes head.h and std::vector
          >
          #include "head.h"
          #include<vector >
          #include<iostre am>
          I would include my own headers after the standard headers.
          2) head.h
          A header file that contains the class A.
          >
          template <typename T >
          class A {
          public:
          T t;
          >
          A(T t_){
          t = t_;
          }
          int getValue() {
          int res = (*t.begin()).x;
          return res;
          >
          }
          };
          >
          In class A getValue() returns the x field of the struct defined in main.cpp.
          But how does class A know about the type 'P' which is only created in main?
          Because of how the preprocessor and #include works. When the pre-
          processor sees the #include line it replaces that line with the contents
          in the specified fields. This means that when the compiler gets main.cpp
          it contains first the definition of A (followed by contents of vector
          and iostream) and then P and finally main(). So when the compiler tries
          to instantiate A it know everything about both A and P.

          --
          Erik Wikström

          Comment

          • Pete Becker

            #6
            Re: Type visibility

            On 2008-08-04 06:32:28 -0400, "saneman" <asdff@asd.coms aid:
            >
            2) head.h
            A header file that contains the class A.
            >
            template <typename T >
            class A {
            public:
            T t;
            >
            A(T t_){
            t = t_;
            }
            int getValue() {
            int res = (*t.begin()).x;
            return res;
            >
            }
            };
            >
            >
            >
            >
            In class A getValue() returns the x field of the struct defined in main.cpp.
            But how does class A know about the type 'P' which is only created in main?
            "A" is not a class. It is a template. At the point where the code says
            A<containerall the details are known, and the compiler creates the
            class A<container>.

            --
            Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
            Standard C++ Library Extensions: a Tutorial and Reference
            (www.petebecker.com/tr1book)

            Comment

            • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

              #7
              Re: Type visibility

              On 2008-08-04 13:39, Pascal J. Bourguignon wrote:
              "saneman" <asdff@asd.comw rites:
              >
              >I have made two modules:
              >
              Good.
              >
              >
              >1) main.cpp
              >2) head.h
              >
              Wrong.
              >
              >
              If you have two modules, you should have five files:
              >
              interface implementation
              Module A: ModuleA.h ModuleA.cpp
              Module B: ModuleB.h ModuleB.cpp
              Main: main.cpp
              Or, if you count main as a module you only need on header:

              interface implementation
              Module A: ModuleA.h ModuleA.cpp
              Main: main.cpp

              --
              Erik Wikström

              Comment

              • James Kanze

                #8
                Re: Type visibility

                On Aug 4, 12:32 pm, "saneman" <as...@asd.comw rote:
                I have made two modules:
                1) main.cpp
                Contains a main function and struct P.
                Includes head.h and std::vector
                #include "head.h"
                #include<vector >
                #include<iostre am>
                struct P {
                int x;
                int y;
                };
                int main() {
                typedef std::vector<Pco ntainer;
                container v;
                P p;
                p.x = 22;
                p.y = 33;
                v.push_back(p);
                A<containera(v) ;
                int res = a.getValue();
                std::cout << res << std::endl;
                return 0;
                }
                2) head.h
                A header file that contains the class A.
                template <typename T >
                class A {
                public:
                T t;
                >
                A(T t_){
                t = t_;
                }
                int getValue() {
                int res = (*t.begin()).x;
                return res;
                }
                };
                In class A getValue() returns the x field of the struct
                defined in main.cpp. But how does class A know about the type
                'P' which is only created in main?
                As Pete said, there is no class A, only a class template A (and
                later, a class A<container>). When parsing a template
                definition, the compiler divides all names and expressions into
                dependent and non-dependent; anything which is dependent only
                gets looked up when the template is instantiated. The rules as
                to when something is dependent, and when it's not, are fairly
                complicated, as are the rules concerning dependent name look-up.
                I'd suggest you get a good book about templates, such as "C++
                Templates: the Complete Guide" (by Vandevoorde and Josuttis),
                and study it carefully.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                Working...