Thread: Using 'new' to replace GetType() when using generics

  1. #1
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19

    Using 'new' to replace GetType() when using generics

    Greeting.

    I'm doing a project with a heavy amount of generics, and I was wondering what your opinions were on replacing GetType() on generic classes, like in the following example;

    Code:
    class MyClass<T>
    {
        public void SomeFunc(...)
        {
            ...
        }
    
        public new System.Type GetType()
        {
            return typeof(T);
        }
    }
    Is this a good way to do it or should I use some other name instead?
    Like e.g. GetGenericType() to enable getting the type of MyClass too, since the new GetType() replaces it with the type of T..?

    Thanks in advance!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm surprised overriding GetType() is even allowed.

    Either way, I consider it a terrible idea. You're changing the semantics of a very important function.
    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

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I agree with CornedBee. Sounds like a really bad idea.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19
    Quote Originally Posted by pianorain
    I agree with CornedBee. Sounds like a really bad idea.
    That's what I thought too. Just wanted to hear other people's opinions.

    Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace Array
    By SARAHCPS in forum C Programming
    Replies: 9
    Last Post: 11-15-2005, 11:07 AM
  2. Replace a list with a new entry?
    By niponki in forum C Programming
    Replies: 4
    Last Post: 08-17-2005, 10:41 AM
  3. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  4. How to replace strings in a file?
    By Doh in forum C Programming
    Replies: 6
    Last Post: 11-26-2002, 12:16 PM
  5. Need a new way to replace a constant
    By RustGod in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2001, 03:05 PM