Thread: Static templated method problem

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Static templated method problem

    Hi, I'm trying to do a class that will be able to allocate an array of derives class
    by having an array of pointer of a root class. Then I'd call a method that receives
    a template representing the child class to affect on one of the pointers in the array.
    So I could be able to call new with a template to allocate different kind of pointers.

    I don't really know how to do this but I started with void pointers. So I want to be
    able to pass the template with the method to allocate one of the pointer to data
    of T type, that will change every time the method is called. I don't want the template
    to affect the whole class, in the way that the class will only hold one type...

    But I get errors because the method has to be static and then I don't have the
    rights to use NEW on non-static data or something...


    Code:
    class foo
    {
    	void	*a[10];
    	int		b;
    	public:
    		template <class T>
    		static void func(void)
    		{
    			a[b] = new T;
    			b++;
    		}
    };

    how could I do this?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Static member functions cannot access non-static members. You basically have three options:
    1) Make the function non-static
    2) Make the data static
    3) Give the static function an instance of the class to work with (e.g., by passing an argument to the function)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well if my class's var get static, I won't be able to use NEW.
    I can't give an instance of what I'm working with as I will always be passing
    different kind of derivate classes...

    Is there really no solution to this?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Well if my class's var get static, I won't be able to use NEW.
    Why not?

    >> Is there really no solution to this?
    There probably is, but it is very difficult to tell what you are really trying to do. Chances are one of JaWiB's three suggestion is really what you should be doing.

    Why does the function have to be static?

  5. #5
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well it's not that I need it to be static, but I get
    errors if I don't write "static" in front of my method
    as soon as I use a template for a method...

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you can provide an example that shows the errors and the error messages themselves perhaps someone can help identify what the problem is.

  7. #7
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    I don't understand why, but today I tried again to compile without
    writing static, and then it worked... I don't see why yesterday it wouldn.t
    work, because I made no modifications to my code...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Static member (map) / constructor initialisation problem
    By drrngrvy in forum C++ Programming
    Replies: 9
    Last Post: 12-28-2005, 12:03 PM
  4. Problem either on display method or structure..
    By DarrenY in forum C Programming
    Replies: 2
    Last Post: 09-04-2005, 08:16 AM
  5. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM