DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: template class constructor definition

Hybrid View

  1. #1
    Join Date
    Oct 2007
    Posts
    369

    template class constructor definition

    Is one or the other of these right or wrong?
    Code:
    template<int MAX>
    class B
    public:
         B();
    };
    
    B<MAX>::B() { ... }
    or

    Code:
    template<int MAX>
    class B
    public:
         B();
    };
    
    B<MAX>::B<MAX>() { ... }
    (Note the use of template parameter in the definition of the constructor.)

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    What is it that you need? A partial specialization? It's unusual to declare templates like this and the code in both cases is invalid. This is the closest thing I can think of: a template specialization that's based on an integral value:
    Code:
    template<int>
    class B{
    public:
         B();
    };
    
    template <int>
    B<5>::B() { }
    int main()
    {
     B<5> b;
    }
    Anyway, the token MAX is redundant unless it's a predefined constant.
    Last edited by Danny; 05-14-2010 at 02:54 PM.
    Danny Kalev

  3. #3
    Join Date
    Oct 2007
    Posts
    369
    I guess I didn't word the question very well. I want to make the class declaration cleaner (smaller), so I'm specifying the body of the functions after the class declaration (instead of inline).
    So, do my examples make sense in that context?

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    Quote Originally Posted by hendrixj View Post
    I guess I didn't word the question very well. I want to make the class declaration cleaner (smaller), so I'm specifying the body of the functions after the class declaration (instead of inline).
    So, do my examples make sense in that context?
    the issue is <max> where you probably meant <int> in the examples (?) Not totally sure.

    Also, not sure how any of this will make anything any "smaller". It just seems to rearrage it, not reduce it or even provide much in the way of cleanup. Maybe we need to see a better example of a messy template that becomes nice and clean thru your methods to understand what you are going for.

  5. #5
    Join Date
    Oct 2007
    Posts
    369
    Ok. Let me try again. I could define the template this way (in a single file):
    Code:
    template<int MAX>
    class B
    public:
         B() { .. }
         int somefunction() { ... }
    ...
    };
    That is, I could put the body of the member functions inline with the templated class declaration. But if the code for the member functions get long, perhaps I want to have a "short and sweet" class template declaration, and then put the definition of the template functions below the class declaration.
    Something like this:
    Code:
    template<int MAX>
    class B
    public:
         B();
         int somefunction();
    ...
    };
    
    B<MAX>::B() { ... }
    
    int B<MAX>::somefunction() { ... }
    ...
    It's still all in one file, but someone could look at the top of my file and see the class interface without having to weed through all of the code.
    So, my question was about how to specify the constructor:
    B<MAX>::B<MAX>() {... }
    or
    B<MAX>::B() { ... }

    I had it the first way, and g++ 3.3 compiled it without complaint. But 3.4 wants the second way. I know that g++ isn't a very good guide to what is actually legal/correct, so I was asking y'all which is correct.

  6. #6
    Join Date
    Oct 2007
    Posts
    369
    I might add that page 187 of "C++ in a nutshell" (2003) implies that both are correct.

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    The correct form is
    Code:
     B<arg>::B(){}
    Code:
    template<class T>
    class B{
    public:
         B();
    };
    //template <class T> class B;
    B<int>::B()
    {}
    Notice that all compilers insist that every translation unit shall include the *definitions* of the template's functions, so there's not much point in moving member functions outside the class body.
    Danny Kalev

Similar Threads

  1. Replies: 27
    Last Post: 02-11-2016, 06:29 AM
  2. Problem with Matrix template class
    By greenmile in forum C++
    Replies: 2
    Last Post: 10-16-2008, 10:09 PM
  3. Replies: 2
    Last Post: 05-30-2008, 12:21 AM
  4. Using a new type in a template class
    By maksoud in forum C++
    Replies: 2
    Last Post: 08-23-2006, 12:54 PM
  5. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center