CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: <types>

  1. #1
    Join Date
    May 2011
    Posts
    23

    <types>

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    int type;
    template<class T>
    class templatetask
    {
    public:
        inline T addnumbers (T x1, T y1)
            {
                    m_x = x1;
                    m_y = y1;
                    return (m_x+m_y);
            }
    private:
        T m_x;
        T m_y;
    };
    
    int main () 
    {
       if (type == 1)
       {
       templatetask<int> x;
       }
       else
       {
       templatetask<double> x;
       }
      //to do stuff ...the problem is that, outside of if statement,  "x" can't get accessed to. any simple way 
       //to enhance this code to be able t access x this way?.
       return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: <types>

    I suppose you could give both instantiations a common base class....however, the correct solution really depends upon your goal.

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

Featured