CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2010
    Posts
    2

    return type of derived class

    Is it possible to have a base-class method designate a return-value of "the" derived class, and then create and return an instance of (or "relative to") whichever derived class it's called from?

    Code:
    abstract class Base {
      public static List<*DerivedClass*> BuildFromList(List<String> in_s) {
        List<*DerivedClass*> rv = new List<*DerivedClass*>();
        foreach (String s in in_s) {
          rv.Add(new *DerivedClass*(s));
        }
      }
      return rv;
    }
    Can this be done, or am I better off just using generics anyway?

    Thanks.

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: return type of derived class

    The only way to do this is to use generics. Otherwise you can't define the type of the return value.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jun 2010
    Posts
    2

    Re: return type of derived class

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured