473,387 Members | 3,787 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,387 software developers and data experts.

C++ inheritance not working correctly in VS2005 when C# is involved.

It can't be... say it isn't so...

Yes.. there is a fly in the ointment

This bug requires 3 projects... yes this is a real world production
issue not some tinker toy thing.

Start with a C# interface assembly we will call Interfaces
namespace Interfaces
{
public interface ICqBase
{
void baseIt(); // does something
};

public interface ICqPolygonObject : ICqBase
{
void polygonObject(); // does something to polygons
};
public interface ICqStairObject : ICqPolygonObject
{
void stairObject(); // does something to stairs
};
}

Then assume you have a managed C++ assembly we will call TestLibrary
the oldClrSyntax switch is being used
its namespace looks as follows:
namespace TestLibrary
{
//
// This class is needed to reproduce the error.. I dont know why
//
public __gc class CqBase : public ICqBase
{
public: virtual void baseIt(void) { return; }
};
public __gc class CqPolygonObject : public CqBase, public
ICqPolygonObject
{
public: virtual void polygonObject() { return; }
};
}
So far this is very real world.. and everything works just fine.
Now lets assume we have another manged C++ assembly we will call
DerivedLibaray
the oldClrSyntax switch is being used also.
namespace DerivedLibaray
{
//
// All the problems are here with calling baseIt
//
public __gc class CqStairObject : public CqPolygonObject , public
ICqStairObject
{
public: virtual void stairObject() { this->baseIt(); return;
}
};
}
Notice we inherit from bother an inherited interface and a class with
inherited interfaces as well.
This shouldn't be a problem because Managed C++ inherits interfaces
virtually.
However the VS2005 compiler generates the folowing error
3>h:\data\code\testlibrary\derivedlibrary\DerivedL ibrary.h(43) : error
C2385: ambiguous access of 'baseIt'
3> could be the 'baseIt' in base 'TestLibrary::CqPolygonObject'
3> or could be the 'baseIt' in base 'Interfaces::ICqStairObject'

Hmm thats odd... how is this ambigous.. you can't call baseIt in
ICqStairObject it's an interface?

Many change seem to make the problem go away.
Changing the interfaces to C++ fixes the issue.. except my interfaces
project has as over 200 interfaces.

removing the CqBase class fixes the issue but I really need this class
in my code because it has more than the baseIt method.

Moving the CqStair class to the TestLibrary from DerivedLibrary also
fixes things.
However, I have 76 projects for a reason. There are multiple products
and product version generated from the code and its just not feasible
to consolidate. Trust me on this one.

Converting the code to C++/CLI fixes the problem. I would use this
technique, if I didn't have 50 C++ projects with over 500,000 lines of
managed code using clrOldSyntax.

Anyone have any ideas for some other workaround.

This has been submitted to the VC team as a bug.

Thanks for the help

Christopher

Jul 1 '06 #1
9 1297
Hello Christopher,
It can't be... say it isn't so...

Yes.. there is a fly in the ointment

This bug requires 3 projects... yes this is a real world production
issue not some tinker toy thing.

Start with a C# interface assembly we will call Interfaces
...
I've reproduced the problem and got the "ambiguous access of 'baseIt'". To
resolve it I've called "baseIt" using fully qualified name:
"this->CqBase::baseIt();". This helped immediately.
--
Vladimir Nesterovsky
Jul 2 '06 #2
I've reproduced the problem and got the "ambiguous access of 'baseIt'". To
resolve it I've called "baseIt" using fully qualified name:
"this->CqBase::baseIt();". This helped immediately.
--
Vladimir Nesterovsky

Vladimir ,

Thanks for the help.. and that does get things compiling right away.

However, doesn't this make the function non virtual?

By adding the scope resolution operator assumes you know what class has
the final derived implementation of the baseIt method. Isn't it
possible that the method will be overridden by another class?

For example; if someone where to derive from CqStairObject to create
say CqEscalator and needed to override baseIt ( for reasons other than
I can think of quickly ). Doesn't the scope resolution operator
you've added make things behave as non virtual?
Christopher

Jul 3 '06 #3
Okay,

Now I'm replying to myself...

This is the solution from Microsoft support
---- begin ----
This was a bug in VC++ 2003. The compiler should have never allowed
this code to compile. The root of the problem is that the C# name
lookup rules (and hence the CLR name lookup rules) specify that except
when name lookups start in an interface all interface members are
ignored. This means that the compiler should never find the baseIt
member in the interface. The problem is that this is contrary to how
ISO/IEC C++ specifies that name lookup should work and hence we did,
for a couple of releases, resist making this change. But with C++/CLI
we decided to bite the bullet and accept the C# name-lookup rules for
C++/CLI classes. We decided that we would only do this for C++/CLI we
would leave the Managed extension as they were. Unfortunately, as I
have said, it appears that there is a bug in Visual C++ 2003 in which
it appears to follow the C# name-lookup rules.

The best workaround I can see is to add a cast-node - for example:
public: virtual void stairObject() {
static_cast<CqPolygonObject*>(this)->baseIt(); return; }
and
CqStairObject* myStair = new CqStairObject();
static_cast<CqPolygonObject*>(myStair)->baseIt();
--- end ---

This does seem to solve the virtual issue because they are using a cast
instead of the scope resolution operator so you don't lose the virtual
nature of the function..
but it's very ugly.

So.. here is my next question...

IS MICROSOFT TRYING TO KILL C++ ?

The decision making on name lookup is nothing short of psychotic or
intentionaly trying to mess with C++ users.

In 2003 Managed C++ was contrary to the ISO spec when utilizing the NON
ISO keywords __interface
This sounds acceptiable to me because it makes things work in the
..NET enviorment where the ISO standard would break things.
Especially under the circumstances where __inteface is a Managed
extension not covered in the ISO spec.
In ISO C++ interfaces are just a concept not an implimentation.

In 2005 Microsoft decided to break old managed code ( which is being
used in VS2005 now as a compatibility bridge to C++/CLI) to favor the
ISO spec when using a NON ISO keyword (which in tern breaks compatibliy
with old Managed code ).
Did this decision come from someone who's next words were "Fuck
our Managed C++ users" ( sorry ).

In 2005 C++/CLI ( which doesn't have any compatibility issues )
Microsoft decide to "bite the bullet" and not conform to the ISO
standard and revert to the C# spec again.
One only has to wonder if this will change this in the next
release to satisfy the ISO standard when using NON ISO keywords.

I'm really not sure how you could have more elegantly messed with C++
uses.

Sorry, its just very sad.. I've worked with VS since VS5 and I feel
very betrayed.

Christopher

Jul 3 '06 #4
IS MICROSOFT TRYING TO KILL C++ ?
....
In 2005 C++/CLI ( which doesn't have any compatibility issues )
Microsoft decide to "bite the bullet" and not conform to the ISO
standard and revert to the C# spec again.
One only has to wonder if this will change this in the next
release to satisfy the ISO standard when using NON ISO keywords.
Microsoft is definitly not trying to kill C++.

When managed extensions was released, Microsoft got a lot of negative
feedback because the syntax was butt-ugly and confusing.

C++/CLI was designed to be comprehensible and easy to use.
IMO it is a lot better than managed extensions, but all the new keywords
make it non-ISO C++.
Since C++/CLI was designed specifically for working with .NET, it would make
sense to play by the .NET rules so that the behavior in 1 .NET language would
be similar to the behavior in other .NET languages.

This document is a good read if you are working with C++/CLI:
www.gotw.ca/publications/C++CLIRationale.pdf
it was written by Herb Sutter to explain the rational behind some of the
C++/CLI design decisions

C++ is alive and kicking, and will be so for a long time.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Jul 4 '06 #5
Bruno van Dooren wrote:
>
Microsoft is definitly not trying to kill C++.

When managed extensions was released, Microsoft got a lot of negative
feedback because the syntax was butt-ugly and confusing.

C++/CLI was designed to be comprehensible and easy to use.
IMO it is a lot better than managed extensions, but all the new keywords
make it non-ISO C++.
Since C++/CLI was designed specifically for working with .NET, it would make
sense to play by the .NET rules so that the behavior in 1 .NET language would
be similar to the behavior in other .NET languages.
This is acceptable to me. When you create a ref/value class in C++/CLI,
you aren't creating a C++ object, but a .Net object so there will be
differences in behaviour, and sometimes this difference may come at
compile time.

We're just in the process of evaluating the effort required to move our
projects from Borland C++Builder to C++/CLI with C# UIs, and so are used
to this sort of issue. Borland's VCL (very similar to the .net
librarys) is written in pascal, so even in C++, if you create a
derivation of TForm in C++, we get a pascal style object which behaves
differently in certain respects to a C++ object (when the vtable is
constructed is one big difference).

I think there has to be compromises when people want all this
functionality such as reflection and automatic garbage collection that
isn't available through standard C++.

Cheers

Russell
Jul 4 '06 #6
Okay guys,

So you think this C++/CLI stuff is the cat meow and proves Microsoft's
commitment to C++.

I tend to agree that the syntax is better than Managed C++.
It is definitely much similar to other .NET languages like C#.

However here is the business rub.

If I have a large investment in Managed C++ and VS2005 compiles it
almost as well as GCC or some other Unix compiler does, what does a
manager do?

1. Stay behind with VS2003 - no cost
2. Port from Managed C++ to C++/CLI - high cost
3. *Port from Managed C++ to C# - high cost

My guess is that most managers will opt for option number 3 (if there
are resources) not 2. Number 1 will be the option of companies pressed
for cash.

This is the big reason that I think C++ incompatibilities are going to
really mess with C++. C++/CLI may be great for someone just starting a
new .NET project. But if you have already been building a large C++
application you're now in a very dark hole.

And I sorry.. but its very clear that Microsoft's support for
clrOldSyntax in VS2005 just isn't there. I have experience porting
code and I made less changes moving windows C++ code to Unix with
Mainsoft's libraries than what I've done to get VS2003 C++ to compile
in VS2005.

The lack of compatibility between VS2003 and VS2005 for C++ is just a
subtitle but clear message to management of software vendors. The
message is this; if you want to keep developing in C++ we will happily
provide products for you that will mess with you're business.

However, I haven't had to make a single change to a C# module while
moving from VS2003 to VS2005 and the message is very clear, if you're
making business decisions.
Sorry,
Christopher

Jul 5 '06 #7
DrZogg wrote:
Okay guys,

So you think this C++/CLI stuff is the cat meow and proves Microsoft's
commitment to C++.
I didn't say that at all. My point was that when you create a 'ref
class' in C++/CLI, you must expect some differences either at runtime,
compile time (as in this case), or both, compared to a 'class' object.

Cheers

Russell
Jul 5 '06 #8
If I have a large investment in Managed C++ and VS2005 compiles it
almost as well as GCC or some other Unix compiler does, what does a
manager do?
GCC or whatever does not compile managed extensions at all.
>
1. Stay behind with VS2003 - no cost
2. Port from Managed C++ to C++/CLI - high cost
3. *Port from Managed C++ to C# - high cost

My guess is that most managers will opt for option number 3 (if there
are resources) not 2. Number 1 will be the option of companies pressed
for cash.
I know several companies that are still developing in VS2003 and even some
that require VC6.
this has not so much to do with money, as with 'if it ain't broken, don't
fix it'
This is the big reason that I think C++ incompatibilities are going to
really mess with C++. C++/CLI may be great for someone just starting a
new .NET project. But if you have already been building a large C++
application you're now in a very dark hole.

And I sorry.. but its very clear that Microsoft's support for
clrOldSyntax in VS2005 just isn't there. I have experience porting
code and I made less changes moving windows C++ code to Unix with
Mainsoft's libraries than what I've done to get VS2003 C++ to compile
in VS2005.
It depends. I have helped some people port medium size projects to VC2005
via clrOldsyntax.
sure, they needed some tweaking and small changes (the projects, not the
people) but on the whole
the process was fairly painless.
The lack of compatibility between VS2003 and VS2005 for C++ is just a
subtitle but clear message to management of software vendors. The
message is this; if you want to keep developing in C++ we will happily
provide products for you that will mess with you're business.
Microsoft told developers very early on that managed extensions was not
future proof, and that
a much more natural syntax was in development. C++/CLI is meant to stay.
it's not as if this came out of the blue.
However, I haven't had to make a single change to a C# module while
moving from VS2003 to VS2005 and the message is very clear, if you're
making business decisions.
C# was developed specifically for .NET, so it makes sense that they get it
right the first time.

As for business decisions, it all depends on the goal, infrastructure and
other considerations.
I personally prefer C# for .NET class libraries and applications unless I
have to do something where C++/CLI has an advantage, like reusing existing
code or interoperability with unmanaged software.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Jul 5 '06 #9
So.. Microsoft still insist this isn't a bug and here is my response.

----------------------------------------------------------------------------------------------------

I believe that the responding technician doesn't fully understand the
problem as reported.

A. ISO/IEC C++ doesn't define an __interface keyword so how could name
lookup for an interfaces (a Microsoft Managed extension to ISO/IEC C++)
be contrary to the ISO/IEC standard?

B. The error in the code requires that two C++ assemblies be involved.
If the CqStairObject class is moved to the middle assembly then
everything compiles just fine (Please note that a commented example of
this is included in TestLibrary.zip). So this BUG involves there being
more than one managed C++ assembly not simply a difference in the
lookup technique the compiler is using. If this was the case you would
see this error on a much more consistent basis.

C. The proposed work around is very expensive and very difficult to
explain when it should be applied, due to the inconsistent nature of
this bug.
Christopher

Jul 7 '06 #10

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

Similar topics

4
by: T. Kaufmann | last post by:
Hi there, A simple but important question: How can I initialize a super class (like dict) correctly in my subclass constructor? A sample: class MyClass(dict): def __init__(self):
9
by: Peter Jakobi | last post by:
Hello, i am looking for a tool, which dumps inheritance information of c++ binaries. Does such a tool exist?? Can I retrieve interitance from the output of objdump or something else? Does...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
3
by: Andre Ranieri | last post by:
I downloaded VS 2005 web express beta 2 the other day for the purposes of learning more about it and seeing what is involved with converting my ASP.NET code base from 1.1 to 2.0. I've read a few...
15
by: scorpion53061 | last post by:
I am just now really starting to get into inheritance - controls, datasets, views all that stuff and I am seeing the enormous savings in performance and even watching the exe file size go down...
5
by: jao | last post by:
My company's product uses Postgres 7.4.3. Postgres is working well for us, and we've worked through many performance issues by tweaking the schema, indexes, and posgresql.conf settings. ...
1
by: GaryDean | last post by:
I notice that there is still no tab control in vs2005. That seems like a strange omission. Are we left with the only choice to use those I.E. Webcontrols? (maybe it's still because we don't...
0
by: =?Utf-8?B?SGVsZ2U=?= | last post by:
Hi! I have run into a problem with visual inheritance using the VS2005 designer for Windows Forms. My goal is to put business logic and common rules for our controls into its own layer (base...
8
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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...
0
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,...

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.