Header files & Template Class

This is a discussion on Header files & Template Class within the C++ Programming forums, part of the General Programming Boards category; I have made a simple template class: Code: template <typename T1> class POSITIONAL { public: POSITIONAL(T1 varOne, T1 varTwo); POSITIONAL(); ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Header files & Template Class

    I have made a simple template class:

    Code:
    template <typename T1>
    class POSITIONAL
    {
        public:
            POSITIONAL(T1 varOne, T1 varTwo);
            POSITIONAL();
            ~POSITIONAL();
            void operator= (POSITIONAL& rhs);
            T1 posX;
            T1 posY;
    };
    
    
    template <typename T1>
    POSITIONAL<T1>::POSITIONAL(T1 varOne, T1 varTwo)
    {
        posX = varOne;
        posY = varTwo;
    };
    
    template <typename T1>
    POSITIONAL<T1>::POSITIONAL()
    {
        posX = 0;
        posY = 0;
    };
    
    template <typename T1>
    void POSITIONAL<T1>::operator= (POSITIONAL<T1>& rhs)
    {
        posX = rhs.posX;
        posY = rhs.posY;
    }
    
    template <typename T1>
    POSITIONAL<T1>::~POSITIONAL()
    {
    };

    Ok, so I have a few questions.

    1) Should each class allocate/de-allocate its own memory. If so, when the compiler sets aside memory for a class (containing one such as this, but say this allocates and the 2 variables are pointers), does it set aside further memory or does it know that this class will do it itself?

    2) When you use this class within another class, don't you just need to #include it? Code:Blocks is giving me trouble in not recognizing classes within classes. Keeps telling me "error: 'POSITIONAL' is not a type.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,531
    1) Should each class allocate/de-allocate its own memory. If so, when the compiler sets aside memory for a class (containing one such as this, but say this allocates and the 2 variables are pointers), does it set aside further memory or does it know that this class will do it itself?
    My answer to one is yes. If a class allocates memory, it must deallocate it. Now you said pretend the two member data are pointers. In such a case, the size of the class is at least the size of two pointers, and the compiler will set aside memory for that. The pointers themselves point to the memory you allocate. The size of the memory you allocate has nothing to do with the memory that the compiler will set aside for your class to store it's data.

    2) When you use this class within another class, don't you just need to #include it? Code:Blocks is giving me trouble in not recognizing classes within classes. Keeps telling me "error: 'POSITIONAL' is not a type.
    That's correct. POSITIONAL isn't a type, POSITIONAL<T> where T is some other complete type initializes POSITIONAL is a type.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Code:
    SPRITE::SPRITE(int width, int height):GRAPHIC(width, height)
    {
            POSITIONAL<int> * gfxVelocity = new POSITIONAL(0,0);
    ...
    }
    Here is an example of what I have, but it is not being recognized.



    The size of the memory you allocate has nothing to do with the memory that the compiler will set aside for your class to store it's data.
    So I obviously have a SPRITE class that contains a POSITIONAL. When a SPRITE is initialized, does it set aside memory for a POSITIONAL (even if it handles its own allocation)? This is just a point I have stuck in my head, that memory is being set aside twice for a class within a class, and its murky enough in there.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,531
    It sets aside memory for a POSITIONAL<T>. If that is two pointers big, then the compiler sets aside memory for that.

    The pointers will contain an address as a value, which is where the memory that you allocated is actually stored. Since this allocation happens at run-time, the memory you allocated does not contribute to the size of the class. Only the size of the pointers will contribute to the size of the class.

    But it is important to note, that even if you want those two variables in the class we are talking about to be pointers for the sake of discussion, the fact is that they aren't. A pointer is, for example, T*.

    As for the error about POSITIONAL not being recognized that is because you omitted this piece:

    Code:
    POSITIONAL<int> * gfxVelocity = new POSITIONAL<int>(0,0);
    Put another way, POSITIONAL isn't a type but a template, that only becomes a type after the template is initialized. A separate class will be initialized with all the Ts defined as ints ... or doubles... etc.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Make sure you don't forget smart pointers: SourceForge.net: Raw pointer issues - cpwiki
    And I should ask: is there even a need to create a new positional on the heap?
    Btw, it's not good practice to use UPPERCASE names for types since they can clash with macros.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference Counting
    By Dae in forum C++ Programming
    Replies: 10
    Last Post: 08-13-2009, 07:34 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21