Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does overriding virtual function differs only by calling convention mean?

Tags:

c++

com

I am trying to implement IUnknown. I followed the instruction to the tee but it isn't working. When I try to compile I get:

Error   2   error C2695: 'testInterfaceImplementation::AddRef': overriding virtual function differs from 'IUnknown::AddRef' only by calling convention  c:\users\seanm\desktop\test\test\source.cpp 6   1   test
Error   3   error C2695: 'testInterfaceImplementation::QueryInterface': overriding virtual function differs from 'IUnknown::QueryInterface' only by calling convention  c:\users\seanm\desktop\test\test\source.cpp 14  1   test
Error   4   error C2695: 'testInterfaceImplementation::Release': overriding virtual function differs from 'IUnknown::Release' only by calling convention    c:\users\seanm\desktop\test\test\source.cpp 22  1   test

from this code:

#include <Windows.h>
#include <tchar.h>

class testInterfaceImplementation : public IUnknown {
    protected:
        ULONG AddRef() 
        {
            MessageBox(NULL,
                _T("TEST1"),
                _T("TEST1"),
                NULL);
            return 0;
        }
        HRESULT QueryInterface(IN REFIID riid, OUT void **ppvObject) 
        {
            MessageBox(NULL,
                _T("TEST2"),
                _T("TEST2"),
                NULL);
            return S_OK;
        }
        ULONG Release() {
            MessageBox(NULL,
                _T("TEST3"),
                _T("TEST3"),
                NULL);
            return 0;
        }
};
like image 324
user1873073 Avatar asked May 23 '13 16:05

user1873073


People also ask

What is difference between function overriding and virtual function?

The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class.

Can you override a virtual function?

You can override virtual functions defined in a base class from the Visual Studio Properties window.

Do I need to override virtual functions?

It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used.

What if virtual function is not overridden?

In addition, if you do not override a virtual member function in a derived class, a call to that function uses the function implementation defined in the base class. A function that has a deleted definition cannot override a function that does not have a deleted definition.


1 Answers

Add STDMETHODCALLTYPE for each of the methods.

ULONG STDMETHODCALLTYPE AddRef() 
HRESULT STDMETHODCALLTYPE QueryInterface(IN REFIID riid, OUT void **ppvObject) 
ULONG STDMETHODCALLTYPE Release() 

The base class(IUnknown) methods are declared as STDMETHODCALLTYPE (which is a macro for __stdcall). When you override a virtual method, it has to have the same calling convention as the original which in this case is __stdcall

like image 155
user93353 Avatar answered Sep 22 '22 08:09

user93353