Template Pointer Type....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nadeem Afroz
    New Member
    • Jan 2008
    • 11

    Template Pointer Type....

    Hello Folks...

    Just going through the templates... found one good question worth sharing

    C++ allows the syntax type

    template <class T>
    T * ptr;

    can this ptr be used to point to types likes int variable or float variable

    ex: int i =10;
    ptr = &i;

    If this can be done plz share the syntax of doing it...
    Comments will be appreciated...
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    I'm not quite sure what you're looking for here...but anyways...
    The *ptr can point out T-objects, the T object can be an int, float or Whatever just as long as you specify it when you use the template.

    /MK

    Comment

    • Nadeem Afroz
      New Member
      • Jan 2008
      • 11

      #3
      Originally posted by MarkoKlacar
      Hi,

      I'm not quite sure what you're looking for here...but anyways...
      The *ptr can point out T-objects, the T object can be an int, float or Whatever just as long as you specify it when you use the template.

      /MK
      Hello ....
      Can you just give me the syntax of how to do that ...
      i have created
      template <class T>
      T * ptr;

      main()
      {
      int i =10;
      //Here i want to assign the address of i to ptr & display value using ptr

      float j = 20.23;
      //here i want to assing the address of j to ptr & display value using ptr

      return 0;
      }

      Comment

      • MarkoKlacar
        Recognized Expert Contributor
        • Aug 2007
        • 296

        #4
        Hi,

        Check out this link.

        /MK

        Comment

        • Nadeem Afroz
          New Member
          • Jan 2008
          • 11

          #5
          Originally posted by MarkoKlacar
          Hi,

          Check out this link.

          /MK
          already went through this link ... didnt find anything related to this problem...

          Comment

          • MarkoKlacar
            Recognized Expert Contributor
            • Aug 2007
            • 296

            #6
            Hi,

            I hope I understood your question correctly. What you're trying to do is declare a template<class> , och then point it out with a template<class> pointer.

            Is that correct?

            /MK

            Comment

            • Nadeem Afroz
              New Member
              • Jan 2008
              • 11

              #7
              Originally posted by MarkoKlacar
              Hi,

              I hope I understood your question correctly. What you're trying to do is declare a template<class> , och then point it out with a template<class> pointer.

              Is that correct?

              /MK
              Hi ...

              I want to assign the address of variable i to ptr(template type) and then display the value using ptr... any idea how to do that ??

              Comment

              • Nadeem Afroz
                New Member
                • Jan 2008
                • 11

                #8
                Originally posted by Nadeem Afroz
                Hi ...

                I want to assign the address of variable i to ptr(template type) and then display the value using ptr... any idea how to do that ??
                Hello ... are u there ??

                Comment

                • MarkoKlacar
                  Recognized Expert Contributor
                  • Aug 2007
                  • 296

                  #9
                  Hi,

                  Not quite sure I'm afraid...I think you have to override some operator, don't quite remember...sorr y.

                  /MK

                  Comment

                  • Nadeem Afroz
                    New Member
                    • Jan 2008
                    • 11

                    #10
                    Originally posted by MarkoKlacar
                    Hi,

                    Not quite sure I'm afraid...I think you have to override some operator, don't quite remember...sorr y.

                    /MK
                    ok let me know if u remember... thanx...

                    Comment

                    • Sirmont
                      New Member
                      • Jul 2008
                      • 12

                      #11
                      Posted by accident. Use below post.

                      Comment

                      • Sirmont
                        New Member
                        • Jul 2008
                        • 12

                        #12
                        Originally posted by Nadeem Afroz
                        C++ allows the syntax type

                        template <class T>
                        T * ptr;
                        NO... It doesn't. This is not a valid template definition. Are you trying to do this:

                        Code:
                        template<typename T>
                        class MyClass
                        {
                         T* m_Ptr;
                        };
                        If so, the syntax would be:

                        Code:
                        float i = 10;
                        MyClass<float>* c;
                        c.m_Ptr = &i;

                        Comment

                        Working...