Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2004

Tip: Looking for answers? Try searching our database.

generic question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kapil - 20 Sep 2004 07:21 GMT
For the following code

interface class I{};
public ref struct R1 : I
{
R1(R1%){}
R1(){}
};

generic <class T> // if you replace it with template, it works.
void f(T t)
{
     I ^hi = t; // error in this line
}

int main()
{
R1 ^obj;
I ^hi = obj;
f(obj);
}

t.cpp
t.cpp(11) : error C2440: 'initializing' : cannot convert from 'T' to 'I ^'
       No user-defined-conversion operator available, or
       Conversion from generic type not allowed

If I replace generic with template it works fine. Can anyone tell what's
going on?
Thanks,
Kapil
Ben Schwehn - 20 Sep 2004 11:20 GMT
> If I replace generic with template it works fine. Can anyone tell what's
> going on?

You will need to constraint the generic type parameter to implement
inter face I like this:

generic <class T> where T:I
void f(T t)
{
      I ^hi = t;
}
have a look at the differences between generics and templates described
here:

http://msdn2.microsoft.com/library/sbh15dya.aspx

hth

Signature

Ben
http://bschwehn.de


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.