Thread: Class Templates question

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Class Templates question

    Hi,

    I am trying to use a template for my classes, eg:

    Code:
    template<class VEHICLE>
    class SportsCar
    {
    //data members/functions
    }
    I am eventually aiming to hold all the objects within a single linked list.

    My question though is that my classes whilst having the same member functions, they also have different data members.

    Is this a problem? The tutorials I have read show the use of Templates, but the data members are always the same, unlike mine.

    Perhaps I should declare all the different data members used by the separate inherited classes within the base class?

    Any advice is appreciated.

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you should describe what is the problem that you are trying to solve before jumping into the use of templates.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Code:
    class Vehicle
    {
    
    };
    
    class Car : public Vehicle
    {
    int MPG;
    int NoOfDoors;
    }
    
    class Motorbike : public : Vehicle
    {
    	int MPG;
    };
    In the code above , the base class is Vehicle.

    The two inherited classes have different data members.

    I want to use a Template (in the hope of storing the different objects within a single linked list).

    Such as:

    Code:
    template<class VEHICLE>
    class Vehicle
    {
    
    };
    
    template<class VEHICLE>
    class Car : public Vehicle
    {
    int MPG;
    int NoOfDoors;
    }
    
    template<class VEHICLE>
    class Motorbike : public : Vehicle
    {
    	int MPG;
    };
    I'm not saying I yet have a problem, I am wondering if having different data members in different classes stops you using a Template for all of the classes.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swerve
    I want to use a Template (in the hope of storing the different objects within a single linked list).
    Your motivation to turn your classes into template classes seems strange: it sounds like you want to use a std::list<Vehicle*>, std::list<std::tr1::shared_ptr<Vehicle> > or boost::ptr_list<Vehicle> instead of involving templates.

    Quote Originally Posted by Swerve
    I'm not saying I yet have a problem, I am wondering if having different data members in different classes stops you using a Template for all of the classes.
    No, it does not.
    Last edited by laserlight; 03-29-2009 at 10:32 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks laserlight

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That would be std::list<std::tr1::shared_ptr<Vehicle> >
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CornedBee
    That would be std::list<std::tr1::shared_ptr<Vehicle> >
    Yes, and corrected accordingly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks very much

    Code:
    std::list<std::tr1::shared_ptr<Vehicle> >
    I read this to mean there should be a shared pointer for the entire Vehicle type, and this pointer will point to all the objects within the list, yes?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Swerve View Post
    Thanks very much

    Code:
    std::list<std::tr1::shared_ptr<Vehicle> >
    I read this to mean there should be a shared pointer for the entire Vehicle type, and this pointer will point to all the objects within the list, yes?
    Not as such. shared_ptr is a "smart pointer" type that handles garbage-collection-type things for you (i.e., if all the shared_ptr's pointing to an object go out of scope, the object is deleted). But each pointer points to one object, and you have a list full of them.

    You don't need anything that fancy to do the basic sort of thing you're trying to do, if you don't want it. A plain old Vehicle* can hold a pointer to all its derived types, without any nonsense about templates or the like.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 03:01 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  4. n00b question regarding the Map Class in the STL library
    By Axegrinder#9 in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 09:40 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM