473,837 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MC++ problem with virtual function overrides and collection editor

In C++ an overridden virtual function in a derived class must have the exact
same signature of the function which is overridden in the base class, except
for the return type which may return a pointer or reference to a derived
type of the base class's return type.

In .NET the overridden virtual function is similar, but an actual parameter
of the function can be a derived reference from the base class's reference
also.

This dichotomy can create a problem when using MC++ to override virtual
properties. In MC++ one can never override a virtual property by using a
derived reference type for the base class's reference type. In .NET one can
specify an overridden property which is a derived reference type from the
base class's reference type.

This dichotomy comes into play when creating a custom collection class in
MC++. The typical custom collection in other .NET languages overrides the
Item type in CollectionBase, not only to provide type-safe usage but to
creating a more specific type than an 'object' reference which the
collection editor in the Component Designer and Windows Form Designer will
understand to allow specific types to be added to a collection.

In MC++, however, it appears that the Item type must still be the 'object'
reference ( Object * in MC++ ) since it is impossible to override the
set_Item function with a different type than an Object * because of
paragraph 1 above. This means that because the get_Item and set_Item must
refer to the same type, the type must be Object * for an MC++ custom
collection class. Of course MC++ can still provide type-safe usage by not
allowing anything but the actual Item object, whatever it is, to be added,
inserted, and removed from the collection. However as far as the collection
editor in the Component Designer and Windows Form Designer is concerned, any
object is a possibility to be added to such a collection.

Is there a way around this difference between MC++ other .NET languages
other than writing one's collections in another .NET language and creating a
separate assembly just for the collection, or accepting the limitation of
MC++ as far as the collection editor is concerned ?
Nov 17 '05 #1
2 2520
Edward,

The .NET Framework 1.1 SDK documentation topic for class CollectionBase
includes a C++ sample, and this sample does compile and run correctly on
the .NET Framework 1.1.

This example does not compile with the C++ compiler that comes with the
..NET Framework 1.0 SDK, however. If I understand correctly, two
differences between VC++ 7.0 and VC++ 7.1 are at play here:
- If a __gc class declares a method whose name and parameter list match
an interface method, then VC++ 7.0 assumes that the programmer wants to
implement the interface method and, thus, requires that the return type
is covariant. VC++ 7.1, however, does not assume this. If the return
type is not covariant, VC++ 7.1 considers both methods to be unrelated.
- If a __gc class declares two methods with the same name and parameter
types, then VC++ 7.0 requires that they are both qualified with an
interface name. VC++ 7.1, however, allows one method to be unqualified.

In short, VC++ 7.0 behaves more like C++ and VC++ 7.1 behaves more like C#.

Your analysis contains some errors. See below.

Edward Diener wrote:
In C++ an overridden virtual function in a derived class must have the exact
same signature of the function which is overridden in the base class, except
for the return type which may return a pointer or reference to a derived
type of the base class's return type.
I agree.
In .NET the overridden virtual function is similar, but an actual parameter
of the function can be a derived reference from the base class's reference
also.
I believe that, for all intents and purposes, this statement is false,
and that in .NET, a method m1 overrides a method m2 only if the
parameter types list and the return type of m1 are equal to those of m2.
In this case, .NET is more strict than C++, and not the other way
around. My belief is supported by all known current implementations , as
well as by the C# Language Specification, which says, in section 10.5.4:
"An override method overrides an inherited virtual method with the same
signature." The same section also says that it is an error if the
override declaration and the overridden base method do not have the same
return type. Interestingly, I was not able to find language in the
Common Language Infrastructure (CLI) specification that supports either
of our views. The CLI says that the callvirt instruction calls the
method that "matches" the name and signature specified in the
instruction. Note that in CLI terminology, the signature includes the
return type. The problem is that "to match" could be interpreted as "to
equal", but it could also be interpreted as allowing covariance.

This is very interesting food for language lawyers, but a more relevant
point is that creating a strongly typed collection class using
CollectionBase does not require covariance.

For example: Please see the Int16Collection example in the 1.1
documentation for CollectionBase. The set_Item(int,In t16) method does
not override the set_Item(int,ob ject) method. These two methods are
unrelated. The Int16Collection class serves its purpose as a strongly
typed collection class, even in the absence of covariance.
This dichotomy can create a problem when using MC++ to override virtual
properties. In MC++ one can never override a virtual property by using a
derived reference type for the base class's reference type. In .NET one can
specify an overridden property which is a derived reference type from the
base class's reference type.
Sentence 2 is true; sentence 3 is false.
This dichotomy comes into play when creating a custom collection class in
MC++. The typical custom collection in other .NET languages overrides the
Item type in CollectionBase, not only to provide type-safe usage but to
creating a more specific type than an 'object' reference which the
collection editor in the Component Designer and Windows Form Designer will
understand to allow specific types to be added to a collection.
Custom collection classes declare an Item property, and this property is
retrieved through reflection by the Designers, but this property does
not override the Item property declared by IList.
In MC++, however, it appears that the Item type must still be the 'object'
reference ( Object * in MC++ ) since it is impossible to override the
set_Item function with a different type than an Object * because of
paragraph 1 above. This means that because the get_Item and set_Item must
refer to the same type, the type must be Object * for an MC++ custom
collection class.
The point is that VC++ 7.0 does not allow a function declaration whose
name and parameter types list are equal to a function in a base type,
except if the declared function overrides the function in the base type.
VC++ 7.1 does allow this (for __gc classes at least).
Is there a way around this difference between MC++ other .NET languages
other than writing one's collections in another .NET language and creating a
separate assembly just for the collection, or accepting the limitation of
MC++ as far as the collection editor is concerned ?


I do not think it is possible in VC++ 7.0.

Greetings

Bart Jacobs

Nov 17 '05 #2
Bart Jacobs wrote:
Edward,

The .NET Framework 1.1 SDK documentation topic for class
CollectionBase includes a C++ sample, and this sample does compile
and run correctly on the .NET Framework 1.1.

This example does not compile with the C++ compiler that comes with
the .NET Framework 1.0 SDK, however. If I understand correctly, two
differences between VC++ 7.0 and VC++ 7.1 are at play here:
- If a __gc class declares a method whose name and parameter list
match
an interface method, then VC++ 7.0 assumes that the programmer wants
to implement the interface method and, thus, requires that the return
type
is covariant. VC++ 7.1, however, does not assume this. If the return
type is not covariant, VC++ 7.1 considers both methods to be
unrelated. - If a __gc class declares two methods with the same name
and parameter types, then VC++ 7.0 requires that they are both
qualified with an interface name. VC++ 7.1, however, allows one
method to be unqualified.

In short, VC++ 7.0 behaves more like C++ and VC++ 7.1 behaves more
like C#.
Thanks for this elucidation.

Your analysis contains some errors. See below.

Edward Diener wrote:
In C++ an overridden virtual function in a derived class must have
the exact same signature of the function which is overridden in the
base class, except for the return type which may return a pointer or
reference to a derived type of the base class's return type.
I agree.
In .NET the overridden virtual function is similar, but an actual
parameter of the function can be a derived reference from the base
class's reference also.


I believe that, for all intents and purposes, this statement is false,


You are correct and it is my error in thinking so.
snipped...
This dichotomy comes into play when creating a custom collection
class in MC++. The typical custom collection in other .NET languages
overrides the Item type in CollectionBase, not only to provide
type-safe usage but to creating a more specific type than an
'object' reference which the collection editor in the Component
Designer and Windows Form Designer will understand to allow specific
types to be added to a collection.
Custom collection classes declare an Item property, and this property
is retrieved through reflection by the Designers, but this property
does
not override the Item property declared by IList.


This is what led to my wrong thinking and misunderstandin g of virtual
function overrides in .NET. I had assumed that the implementations of custom
collection classes were overriding virtual functions in the base
CollectionBase class, without closing looking at the code in the examples or
even noticing that there was a version for MC++ using Int16. They are
actually overloading functionality, using covariant parameter and return
types, and not overriding them. I hadn't made the association that
reflection was at work in the Collection Editor of the Windows Form Designer
and Component Designer rather than normal polymorphism, but there was no
documentation that this was happening. So I naively assumed that .NET
allowed co-variant parameters as well as covariant return types in its
overrides, which was the only way I could think of that custom collection
classes would work at design time. Evidently the Collection Editor assumes
common property and member function names for a custom collection class but
adjusts to the covariant parameters and/or return types for those names
through reflection. In that case, creating a type-safe class in MC++ works
fine, at least with the VC++ 7.1 compiler, without having to resort to
'object' being the common type in all properties and member functions.
In MC++, however, it appears that the Item type must still be the
'object' reference ( Object * in MC++ ) since it is impossible to
override the set_Item function with a different type than an Object
* because of paragraph 1 above. This means that because the get_Item
and set_Item must refer to the same type, the type must be Object *
for an MC++ custom collection class.
The point is that VC++ 7.0 does not allow a function declaration whose
name and parameter types list are equal to a function in a base type,
except if the declared function overrides the function in the base
type. VC++ 7.1 does allow this (for __gc classes at least).


Yes that would force one in .NET 2002 to override, rather than overload,
CollectionBase properties and methods. But I think that there are notes in
the CollectionBase doc pointing out that certain functionality is not
supported in the 1.0 framework.
Is there a way around this difference between MC++ other .NET
languages other than writing one's collections in another .NET
language and creating a separate assembly just for the collection,
or accepting the limitation of MC++ as far as the collection editor
is concerned ?


I do not think it is possible in VC++ 7.0.


Thanks for all the information and for correcting my false impression of how
the Collection Editor worked with custom collection types.
Nov 17 '05 #3

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

Similar topics

1
2474
by: Lou | last post by:
I'm using the Response Filter Class in MS KB article 811162 (http://support.microsoft.com/default.aspx?scid=kb;EN-US;811162) to generate a static htm page from an asp.net template page being filled from a database. The generated static pages are then streamed into FreeTextbox editor for user edits. I want to use a variable for the filename since each user will need a separate temporary template page. The system works fine for a...
0
1937
by: Sundown | last post by:
I am trying to create a custom button control for the web that, when clicked, disables and changes the text of itself and a bunch of other controls (in the collection). My goal is to end up with a button that prevents the user from submitting a form back multiple times and also prevents them from submitting the form and then clicking something else before the postback can finish processing. What I have is a working button that disables...
14
1936
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
0
1647
by: Mike Hofer | last post by:
Hi everyone. I could really use some help. First, the backstory: ===================== I *really* need a 3-state checkbox for my ASP.NET application. Specifically, I need one that lets me set the image for the checkbox portion so that the control conforms to my site's visual style, and that lets me set the style for the text in both the enabled and disabled states. As you might expect, I couldn't find one. So I decided to roll my own.
0
1456
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and the 2nd property is a strongly typed collection class (myCollectionProperty). The collection contains a simple class (myCustomClass) which has 1 text property (TextProperty).
2
2454
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just fine. However, when attempting to assing default values as designtime in the propertygrid, nothing is working on the class object. I know that I am doing it wrong, any ideas what it is? Thanks in advance
0
1561
by: Kenny | last post by:
Help...wimper.. been trying to get a virtualpathprovider to work and I have to be missing something. I want users to be able to enter something like mydomain.com/userentereddirectory/ and have that serve up a file. So I use vpp to code up an initialization even, virturalfile, virtualdirectory and virtualpath provider. I have a simple file for testing testing.aspx (no codebehind) that just sets a label to 'hello world' on pageload. Now...
2
6978
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
5
13396
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
9691
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10891
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10280
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5859
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4480
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.