473,396 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Covariant returns

When implementing .net interfaces in vc++.net, is there anyway to get around
the "error C2392: : covariant returns types are not supported in managed
types" error?
I know how to do this in c#. Is it possible in c++.net 2003? If so, please
show me how? Will it be possible in c++.net 2005(beta?)?
Nov 17 '05 #1
6 1154
Hasani,
When implementing .net interfaces in vc++.net, is there anyway to get around the "error C2392: : covariant returns types are not supported in managed
types" error?
I know how to do this in c#. Is it possible in c++.net 2003?
Unfortunately, no, not directly. What C# does is not actually covariant
return types... it does a trick by which it "renames" the actual method
implementing the interface/base class method, and creates a new one with the
same name, essentialy.
If so, please
show me how? Will it be possible in c++.net 2005(beta?)?


With the new C++/CLI syntax, yes.
Consider this example:

#using <mscorlib.dll>
using namespace System;
public interface class IA
{
public:
Object^ GetIt();
};

public ref class B : public IA
{
protected:

virtual Object^ GetItFromIA() = IA::GetIt
{
return "Some other thing";
}

public:
virtual String^ GetIt() new
{
return "This is the String";
}
};
int main()
{
IA^ obj = gcnew B();
Console::WriteLine(obj->GetIt());
Console::WriteLine(((B^)obj)->GetIt());
}

Note: This is built using the May preview... haven't gotten beta 1 yet ;)

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2
Tomas Restrepo (MVP) wrote:
the "error C2392: : covariant returns types are not supported in managed
types" error?
I know how to do this in c#. Is it possible in c++.net 2003?

Unfortunately, no, not directly. What C# does is not actually covariant
return types... it does a trick by which it "renames" the actual method
implementing the interface/base class method, and creates a new one with the
same name, essentialy.


could you please give me an example how to achive this in C#? I didn't
succeed :(

Stefan
Nov 17 '05 #3
c# example.

class MyObject : ICloneable
{

//by doing this, you're making the method private and in order to access
this method, the object must be (up)casted to the interface
//in question. In this case, an ICloneable.
//e.x.:
// MyObject obj = new MyObject();
//ICloneable clone = (ICloneable) obj;
//clone.Clone();
object ICloneable.Clone()
{
//do whatever u want. usually u call the Clone method that
returns a strong typed object as I'm doing now
return Clone();
}

public MyObject Clone()
{
return (MyObject) MemberwiseClone();
}
}
"Stefan Slapeta" <st************@slapeta.com> wrote in message
news:cc**********@paperboy.Austria.EU.net...
Tomas Restrepo (MVP) wrote:
the "error C2392: : covariant returns types are not supported in managed
types" error?
I know how to do this in c#. Is it possible in c++.net 2003?

Unfortunately, no, not directly. What C# does is not actually covariant
return types... it does a trick by which it "renames" the actual method
implementing the interface/base class method, and creates a new one with the same name, essentialy.


could you please give me an example how to achive this in C#? I didn't
succeed :(

Stefan

Nov 17 '05 #4
Well, this leads me to my next question.

How would u implement the following in c++

lets say in a c# I created

public interface IFoo
{
Int32 GetValue();
}

public interface IBar
{
String GetValue();
}

so here you have 2 interfaces with signatures that only differ by return
type. If I wanted to make a clas FooBar in c++ that implements/inherits both
interfaces, how would I go about doing this. Please provide a code example
as you did b4 but using synatax that will compile in c++.net 7.0/7.1 (no ^,
ref, etc).

"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:u9****************@TK2MSFTNGP09.phx.gbl...
Hasani,
When implementing .net interfaces in vc++.net, is there anyway to get around
the "error C2392: : covariant returns types are not supported in managed
types" error?
I know how to do this in c#. Is it possible in c++.net 2003?


Unfortunately, no, not directly. What C# does is not actually covariant
return types... it does a trick by which it "renames" the actual method
implementing the interface/base class method, and creates a new one with

the same name, essentialy.
If so, please
show me how? Will it be possible in c++.net 2005(beta?)?


With the new C++/CLI syntax, yes.
Consider this example:

#using <mscorlib.dll>
using namespace System;
public interface class IA
{
public:
Object^ GetIt();
};

public ref class B : public IA
{
protected:

virtual Object^ GetItFromIA() = IA::GetIt
{
return "Some other thing";
}

public:
virtual String^ GetIt() new
{
return "This is the String";
}
};
int main()
{
IA^ obj = gcnew B();
Console::WriteLine(obj->GetIt());
Console::WriteLine(((B^)obj)->GetIt());
}

Note: This is built using the May preview... haven't gotten beta 1 yet ;)

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #5
Hasani (remove nospam from address) wrote:
How would u implement the following in c++

[SNIP]

so here you have 2 interfaces with signatures that only differ by return
type. If I wanted to make a clas FooBar in c++ that implements/inherits
both interfaces, how would I go about doing this. Please provide a code
example as you did b4 but using synatax that will compile in c++.net
7.0/7.1 (no ^, ref, etc).


First, this is one place where the new syntax is superior to the old syntax.
You will have better reliability that the compiler can do the right thing in
the new syntax. I'll present both the old and new syntax for completeness.
Here is the new syntax:

using namespace System;

public interface class IFoo {
int GetValue();
};

public interface class IBar {
String^ GetValue();
};

public ref class R : IFoo, IBar {
public:
virtual int G1() = IFoo::GetValue;
virtual String^ G2() = IBar::GetValue;
};

int R::G1() { return 42; }
String^ R::G2() { return "Hello World!"; }

int main() {
R^ r = gcnew R;
IFoo^ f = r;
IBar^ b = r;

Console::WriteLine(f->GetValue());
Console::WriteLine(b->GetValue());
}

And here is the old syntax:

#using <mscorlib.dll>

using namespace System;

public __gc __interface IFoo {
int GetValue();
};

public __gc __interface IBar {
String* GetValue();
};

public __gc class R : public IFoo, public IBar {
public:
virtual int IFoo::GetValue();
virtual String* IBar::GetValue();
};

int R::IFoo::GetValue() { return 42; }
String* R::IBar::GetValue() { return S"Hello World!"; }

int main() {
R* r = new R;
IFoo* f = r;
IBar* b = r;

Console::WriteLine(f->GetValue());
Console::WriteLine(b->GetValue());
}

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
Nov 17 '05 #6
you've just added another subscriber to your blog!!

Thx Bray!

"Brandon Bray [MSFT]" <br******@online.microsoft.com> wrote in message
news:eo**************@TK2MSFTNGP10.phx.gbl...
Hasani (remove nospam from address) wrote:
How would u implement the following in c++

[SNIP]

so here you have 2 interfaces with signatures that only differ by return
type. If I wanted to make a clas FooBar in c++ that implements/inherits
both interfaces, how would I go about doing this. Please provide a code
example as you did b4 but using synatax that will compile in c++.net
7.0/7.1 (no ^, ref, etc).
First, this is one place where the new syntax is superior to the old

syntax. You will have better reliability that the compiler can do the right thing in the new syntax. I'll present both the old and new syntax for completeness.
Here is the new syntax:

using namespace System;

public interface class IFoo {
int GetValue();
};

public interface class IBar {
String^ GetValue();
};

public ref class R : IFoo, IBar {
public:
virtual int G1() = IFoo::GetValue;
virtual String^ G2() = IBar::GetValue;
};

int R::G1() { return 42; }
String^ R::G2() { return "Hello World!"; }

int main() {
R^ r = gcnew R;
IFoo^ f = r;
IBar^ b = r;

Console::WriteLine(f->GetValue());
Console::WriteLine(b->GetValue());
}

And here is the old syntax:

#using <mscorlib.dll>

using namespace System;

public __gc __interface IFoo {
int GetValue();
};

public __gc __interface IBar {
String* GetValue();
};

public __gc class R : public IFoo, public IBar {
public:
virtual int IFoo::GetValue();
virtual String* IBar::GetValue();
};

int R::IFoo::GetValue() { return 42; }
String* R::IBar::GetValue() { return S"Hello World!"; }

int main() {
R* r = new R;
IFoo* f = r;
IBar* b = r;

Console::WriteLine(f->GetValue());
Console::WriteLine(b->GetValue());
}

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Marcin Vorbrodt | last post by:
Can a base class have a virtual method that takes a base pointer/reference as a parameter, and then overwrite this method in a derived class, and change its parameter to be a reference/pointer to...
9
by: Rob. | last post by:
I originally thought this was a compiler error but it seems the standard prohibits the code below. Has anyone got any good ideas about how to code around it? class A; class B; class X; class...
12
by: David Sobey | last post by:
Hi everyone I'm having huge difficulties overriding a virtual function with a function that returns a covariant type. I'd be grateful if someone could show me some code where this is done: - A...
14
by: Stefan Slapeta | last post by:
Hi, this code does not compile in C#: class base_class {} class derived_class : base_class {} class A { public virtual base_class f()
13
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public...
16
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
6
by: miked | last post by:
Why are there still no covariant return types? All searches reveal no workarounds accept for using an interface which is a real pain. I wind up missing this capability almost every time I...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
9
by: Rahul | last post by:
Hi Everyone, I was trying to implement covariant return types and i get a compilation error, class BB : public AA { }; class A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.