Thread: Is it possible to pass a class to a function?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Is it possible to pass a class to a function?

    Can i do something like:

    Code:
    class T
    {
         void doIt(SomeClass)
         {
              //call some static method on SomeClass
         }
    };
    
    int main()
    {
         T a;
    
         //Pass ref to the class itself, rather than an instance
         a.doIt( ::SomeClass );
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    correct me if I am wrong, but, no, it's not javascript. (the compiler needs to know exactly which function to call in doIt(), which is not possible if it doesn't know what class is going to be passed in)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    class Someclass {
         static void method() {};
    };
    
    class T
    {
         void doIt(SomeClass &c)
         {
              //call some static method on SomeClass
              c.method();
         }
    };
    
    int main()
    {
         Someclass d;
         T a;
    
         //Pass ref to the class itself, rather than an instance
         a.doIt( d );
    }
    Last edited by robwhit; 05-01-2008 at 04:25 PM. Reason: all-important typo :D

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by robwhit View Post
    Code:
    class Someclass {
         static void method() {};
    };
    
    class T
    {
         void doIt(SomeClass c)
         {
              //call some static method on SomeClass
              c.method();
         }
    };
    
    int main()
    {
         Someclass d;
         T a;
    
         //Pass ref to the class itself, rather than an instance
         a.doIt( d );
    }
    Actually, that's passing an instance from what i understand. It simply calls the () constructor (which the compiler created for you) to create "d". If you create a constructor for SomeClass with no args and put a printf in it, you'll see it's being called.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    class T
    {
        template <typename C>
        void doIt()
        {
            C::someMethod();
        }
    };
    
    T().doIt<SomeClass>();
    Although in this case, class T is redundant, as this could be implemented as a free function.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    sorry, typo.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    1) Use a simple wrapper and a constant reference to that wrapper. (A class instance would not be passed, only an instance of the wrapper--which need not have a reference to the class itself.)

    2) Use a template method and explicitly pass arguments to the template parameters.

    3) Use a pointer-to-a-function and pass the static method directly.

    4) "Erase" the target function type with something like "functiods" and pass an instance of that object.

    Soma

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    Code:
    class T
    {
        template <typename C>
        void doIt()
        {
            C::someMethod();
        }
    };
    
    T().doIt<SomeClass>();
    Although in this case, class T is redundant, as this could be implemented as a free function.

    YES! thank you, I don't know why I didn't think of templates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 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