Thread: Typedef'ing an array within a function

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    New Hampshire
    Posts
    3

    Exclamation Typedef'ing an array within a function

    Okay, so here's the deal...
    I have a function that has all of the different sorts within it. I want to make the type that I pass over to it a typedef so that it can sort doubles, ints, strings, etc.

    Here is what I have:

    Any help would be most appreciated.

    Thanks,
    Sincerely,
    Phreakuency

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered using 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
    Apr 2007
    Location
    New Hampshire
    Posts
    3

    Didn't even think of it

    Now I just have to go find out how to make one.
    Will that work pretty easily though?
    Thanks for the speedy reply btw.
    Sincerely,
    Phreakuency

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Of course it will work very easily... of course, the beauty of a good sorting function is the ability to sort any type passed to it (user-defined types, even) which will of course require you to understand how all these types are compared. On top of templates, you should look into the concept of callback functions and passing this function as a template parameter (like a std::map does).
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    New Hampshire
    Posts
    3

    I have never really used templates

    I have never really used templates, so would I be able to get a quick run down on them. I looked it up online and stared blankly at the screen for a good 10 minutes.
    Thanks
    ~Phreakuency

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    There are quick run-downs of them all over the internet... but I'll start you off simple. A templated class is a class definition that defines a kind-of "variable type" and uses this within it's properties and methods... now as far as the compiled program is concerned, this means nothing, simple because all that happens during compilation is the templated class is expanded into several individual classes for each required template of the class. That is to say if your program creates instances of myTemplateClass<int>, myTemplateClass<float>, and myTemplateClass<std::string>, then each of those will essentially get clones of the templated class using it's type.

    Now, onto syntax... To template a class, you simple need to preceed the class definition (and definition of its methods) with a line defining the template and it's arguements. It looks something like this:
    Code:
    #include <iostream> // To be used in main()
    
    template <class T>    // This line states the following class is a template that takes one 
                          // templated argument to be called T. Think of it similar to function arguments.
    class myTemplateClass {    // Here is the class that is templated. Remember, in here any instance of
                               // the templated type will be refered to by type T.
       public:
          myTemplateClass(); // Default constructor
          myTemplateClass(const T& data);  // The argument is of type T.
          void setData(const T& data);
          T    getData();
       private:
          T myData;  // Again, of type T.
    };
    
    // Now for each method definition outside of the class definition, you must restate that it is templated
    // so that the definition knows what you're refering to when you reference the type.
    template <class T>
    myTemplateClass<T>::myTemplateClass() { /* Default constructor does nothing */ }
    // Notice that when you reference the classname, even in scoping, you must give it a template parameter.
    // In the case of the definition, the parameter is of the templated type.
    // Continue defining the rest of the class the same way...
    
    template <class T>
    myTemplateClass<T>::myTemplateClass(const T& data) : myData(data) {}
    
    template <class T>
    void myTemplateClass<T>::setData(const T& data) {
       myData = data;
    }
    
    template <class T>
    T myTemplateClass<T>::getData() {
       return myData;
    }
    
    // See? No problem... for the most part it's like a regular class definition,
    // you just have to be hasseled with referencing the template everywhere.
    // Now in main()... Let's start creating instances of the class.
    int main() {
       myTemplateClass<int> intTemplate(5);  // Remember every instance of the class needs the template parameter
       myTemplateClass<char> charTemplate;
       charTemplate.setData('a');
       myTemplateClass<float> *floatTemplatePtr = new myTemplateClass<float>(3.14);
    
       std::cout << "Int: " << intTemplate.getData() << '\n'
                 << "Char: " << charTemplate.getData() << '\n'
                 << "Float: " << floatTemplatePtr->getData() << std::endl;
       std::cin.get();
    
       return 0;
    }
    A few things to note about templates... the class implementation MUST be in the same file as the definition. That means you can't define in a .h and implement in a .cpp. Also, this is a highly simplistic use of templates, which is a very, very deep and powerful tool in C++. There are books written about templates and only templates... I suggest if you want to really know about them, you pick one up. Lastly, I'm sure this is either in the FAQs or tutorials of this site... I don't know why I bothered writing all this for you, I guess I was just bored. Please make sure you check several references before you ask for general help on something like this.
    Last edited by SlyMaelstrom; 04-09-2007 at 02:15 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM