473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VC++ 2005: private virtual functions

Something I don't get it:

Say we have:

ref class Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Base");}
public:
void SomeAccessibleF unction()
{SomeVirtualFun ction();}
};

ref class Derived : public Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Derived" );}
};

int main(array<Syst em::String ^> ^args)
{
Base^ handle = gcnew Derived();
handle->SomeAccessible Function();

return 0;
}

I would expect to get "Derived" as the result, right? However, I get
"Base", and strange warnings during compilation:

".\PrivateVirtu al.cpp(8) : warning C4486: 'Base::SomeVirt ualFunction' :
a private virtual method of a ref class or value class should be marked
'sealed'
..\PrivateVirtu al.cpp(16) : warning C4486:
'Derived::SomeV irtualFunction' : a private virtual method of a ref
class or value class should be marked 'sealed'
"

If I try explicit overriding:

..\PrivateVirtu al.cpp(17) : error C3671: 'Derived::SomeV irtualFunction'
: function does not override 'Base::SomeVirt ualFunction'
..\PrivateVirtu al.cpp(16) : warning C4486:
'Derived::SomeV irtualFunction' : a private virtual method of a ref
class or value class should be marked 'sealed'

What is going on here?

Nov 17 '05 #1
20 2380
Hi Nemanja!
ref class Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Base");}
public:
void SomeAccessibleF unction()
{SomeVirtualFun ction();}
};

ref class Derived : public Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Derived" );}
};

int main(array<Syst em::String ^> ^args)
{
Base^ handle = gcnew Derived();
handle->SomeAccessible Function();

return 0;
}

I would expect to get "Derived" as the result, right? However, I get
"Base", and strange warnings during compilation:

".\PrivateVirtu al.cpp(8) : warning C4486: 'Base::SomeVirt ualFunction' :
a private virtual method of a ref class or value class should be marked
'sealed'


"private" virtual functions are useless! because nobody can overwrite it!

You need to declare your virtual functions at least as "protected" !

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2
> private" virtual functions are useless! because nobody can overwrite it!

Read this article: http://www.gotw.ca/publications/mill18.htm

Nov 17 '05 #3

"Jochen Kalmbach [MVP]" <no************ ********@holzma .de> skrev i
meddelandet news:ef******** ******@tk2msftn gp13.phx.gbl...
Hi Nemanja!
ref class Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Base");}
public:
void SomeAccessibleF unction()
{SomeVirtualFun ction();}
};

ref class Derived : public Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Derived" );}
};

int main(array<Syst em::String ^> ^args)
{
Base^ handle = gcnew Derived();
handle->SomeAccessible Function();

return 0;
}

I would expect to get "Derived" as the result, right? However, I get
"Base", and strange warnings during compilation:

".\PrivateVirtu al.cpp(8) : warning C4486: 'Base::SomeVirt ualFunction'
:
a private virtual method of a ref class or value class should be
marked
'sealed'


"private" virtual functions are useless! because nobody can overwrite
it!

You need to declare your virtual functions at least as "protected" !


But note that this is so for managed code only. In real C++ it would
have worked!
Bo Persson
Nov 17 '05 #4
Even in "old" Managed C++ it works.

Nov 17 '05 #5
It seems to be stupid from the C++ programmer's point of view, but then
..NET was not designed by C++ programmers.

Unfortunately in .NET virtual functions can't be private, they must be
protected. But even that modification won't solve your problem, you even
have to declare the function override to override a virtual function.
The correct code is

ref class Base {
protected:
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Base");}
public:
void SomeAccessibleF unction()
{SomeVirtualFun ction();}
};

ref class Derived : public Base {
protected:
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Derived" );}
};

I share your pain...

Tom

Nemanja Trifunovic wrote:
Something I don't get it:

Say we have:

ref class Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Base");}
public:
void SomeAccessibleF unction()
{SomeVirtualFun ction();}
};

ref class Derived : public Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Derived" );}
};

int main(array<Syst em::String ^> ^args)
{
Base^ handle = gcnew Derived();
handle->SomeAccessible Function();

return 0;
}

I would expect to get "Derived" as the result, right? However, I get
"Base", and strange warnings during compilation:

".\PrivateVirtu al.cpp(8) : warning C4486: 'Base::SomeVirt ualFunction' :
a private virtual method of a ref class or value class should be marked
'sealed'
.\PrivateVirtua l.cpp(16) : warning C4486:
'Derived::SomeV irtualFunction' : a private virtual method of a ref
class or value class should be marked 'sealed'
"

If I try explicit overriding:

.\PrivateVirtua l.cpp(17) : error C3671: 'Derived::SomeV irtualFunction'
: function does not override 'Base::SomeVirt ualFunction'
.\PrivateVirtua l.cpp(16) : warning C4486:
'Derived::SomeV irtualFunction' : a private virtual method of a ref
class or value class should be marked 'sealed'

What is going on here?

Nov 17 '05 #6
Tamas Demjen wrote:
ref class Derived : public Base {
protected:
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Derived" );}
};


Sorry I copied the wrong code:

ref class Derived : public Base {
protected:
virtual void SomeVirtualFunc tion() override
{Console::Write Line(L"Derived" );}
};

Note the override keyword, it indicates that the function is not
replacing Base::SomeVirtu alFunction, but is overriding it.

Tom
Nov 17 '05 #7
Hi Tom.
Unfortunatel y in .NET virtual functions can't be private, they must be protected
But I am pretty sure CLR allows private virtual functions. It works
fine with old MC++.
But even that modification won't solve your problem, you even have to declare the function override to override a virtual function.


Yep, just noticed this in the C++/CLI spec. Actually, I pretty much
like this "controlled virtuality". Just have no idea why to disable
private virtual functions. Any clues?

Nov 17 '05 #8
Nemanja Trifunovic wrote:
Yep, just noticed this in the C++/CLI spec. Actually, I pretty much
like this "controlled virtuality". Just have no idea why to disable
private virtual functions. Any clues?


The reason I don't like this override keyword is that because if you
miss it, the function won't work like a virtual function anymore, and
there's not even a compiler warning. No override keyword == non-virtual
function, even if the virtual keyword is there. It's very frustrating
when you have a virtual function and it's not polymorphic.

If I make the virtual function private, I get the following .NET runtime
exception:

<quote>
An unhandled exception of type 'System.TypeLoa dException' occurred in
Virtual.exe

Additional information: Method 'SomeVirtualFun ction' on type 'Derived'
from assembly 'Virtual, Version=1.0.199 9.26811, Culture=neutral ,
PublicKeyToken= null' is overriding a method that is not visible from
that assembly.
</quote>

This is not a language feature, it's a .NET runtime exception, which
clearly states that overriding a private virtual function is not
supported by the runtime library.

I don't know if it's intentional, but this is happening with VC++ 2005
Beta2.

Tom
Nov 17 '05 #9
You are right - it does throw. However, with VC++ 2003, the following
code works just fine:

__gc class Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(S"Base");}
public:
void SomeAccessibleF unction()
{SomeVirtualFun ction();}
};
__gc class Derived : public Base {
virtual void SomeVirtualFunc tion()
{Console::Write Line(L"Derived" );}
};
int _tmain()
{
Base* handle = new Derived();
handle->SomeAccessible Function();
return 0;
}

Did they introduce this "feature" in CLR 2.0? That would be very
strange, indeed.

Nov 17 '05 #10

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

Similar topics

2
1980
by: hswerdfe | last post by:
I have several Classes that derive from a common Base Class Each of these Classes Has a Differnt Version of a Function with the Same Short name, but different Long Name. See Bellow: // Base Class class CBase { void DoStuff(CBase* other);
0
1097
by: Adriano Coser | last post by:
Hello. I have an ExpandableObjectConverter subclass that I associate to the 'Converter' of a custom property descriptor. After I changed my .net code to the new syntax (VC 2005), the GetProperties method of my converter class is not called anymore by the PropertyGrid. Now my expandable properties always show a property 'Length' with...
1
1048
by: Peter Oliphant | last post by:
I got 2005 Express to prepare for the arrival of my company's MSDN full version. Upon loading my project it was converted successfully, but did warn me modifications would have to be done for it to compile (which I expected). I then compiled, and got all the errors removed, and I was left with two warnings. Both warning regarded my using...
8
2301
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR types. The problem: I have a number of mixed-mode functions which I want reuse. These functions revolve around converting a CLR String to a C++...
17
7202
by: Fabry | last post by:
Hi All, I'm new of this group and I do not know if this is the correct group for my question. I have a DLL with its export library (.lib) wrote in Borland C++ 6. In borland everything is OK and I am able to include the lib and use the class that i have exported creating an instance with new etc... I would like to export this class in...
10
4777
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a pure virtual function body
7
2491
by: Amu | last post by:
Hi i am stuck up in a part of project where i have to inherit the VC++ template classes into C#.net. Please provide me the solution for this .
2
1908
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me the solution.
14
4171
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
1
7467
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...
0
7805
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...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
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
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
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...

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.