Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:53:37+00:00 2026-06-16T00:53:37+00:00

I recently started using C++/Cli for wrapping purposes. Now I’m at a point where

  • 0

I recently started using C++/Cli for wrapping purposes.
Now I’m at a point where I’ve to know more about the internals.

Consider the following code:

Header file (ignoring .NET namespaces for this example):

public ref class BaseyClass
{
    protected:
        delegate void TestMethodDelegate(); // TestMethod delegate
        BaseyClass();                       // constructor            
        virtual void TestMethod();          // member: method
        GCHandle _testMethodHandle;         // member: method handle
};


CPP file (ignoring .NET namespaces for this example):

BaseyClass::BaseyClass()
{
    _testMethodHandle
          = GCHandle::Alloc(
                       gcnew TestMethodDelegate(this, &BaseyClass::TestMethod));
}

void TestMethod()
{
}

Eventually this class will be used as base class (for a DerivedClass) later and the method “TestMethod()” gets overridden and called from unmanaged code through the delegate pointer.

Now the question: Which method will be referenced by the delegate?

BaseyClass::TestMethod();

or

DerivedClass::TestMethod();

Personally I think the “BaseyClass::TestMethod()” will be referenced by the delegate because even when it’s overridden, the delegate points to the (base-)address of BaseyClass. Hence a DerivedClass cannot override the “TestMethod” and use the delegate from BaseyClass.

I just want to be sure. Thanks for your comments and enlightment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:53:39+00:00Added an answer on June 16, 2026 at 12:53 am

    The delegate will be a reference to the derived class’s TestMethod. Even though you’re passing &BaseyClass::TestMethod, that’s a virtual method, you’re also passing this, which is the derived type, and both of those are taken into account when the delegate is created.

    Other notes:

    • TestMethodDelegate doesn’t need to be inside the class definition. The more standard way is to have the delegate outside of the class, just in the namespace. (Or use the existing built-in one, Action.)
    • You don’t need to GCHandle::Alloc (I assume that’s what you meant by Allow). Instead, declare _testMethodHandle as TestMethodDelegate^ (or Action^). In general, you shouldn’t need to deal with GCHandle unless you’re interfacing with unmanaged code, and this code is all managed.

    Here’s my test code:

    public ref class BaseyClass
    {
    public:
        BaseyClass() { this->_testMethodHandle = gcnew Action(this, &BaseyClass::TestMethod); }
        virtual void TestMethod() { Debug::WriteLine("BaseyClass::TestMethod"); }
        Action^ _testMethodHandle;
    };
    
    public ref class DerivedClass : BaseyClass
    {
    public:
        virtual void TestMethod() override { Debug::WriteLine("DerivedClass::TestMethod"); }
    };
    
    
    int main(array<System::String ^> ^args)
    {
        BaseyClass^ base = gcnew DerivedClass();
        base->_testMethodHandle();
        return 0;
    }
    

    Output:

    DerivedClass::TestMethod

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started writing a C++/CLI wrapper for a native c++ library. I'm now
I recently started using the Yii framework trying to learn a little bit more
I recently started using R language and now i am using R for most
I recently started using rspec and factory_girl and I'm working on a basic control
We recently started using AnkhSVN subversion plugin for Visual Studio. Everything is working as
I recently started using jQuery/AJAX for submitting data to my PHP API. Currently what
I recently started using lcov to visualize my code coverage. It's a great tool.
Ive recently started using CodeIgniter as I was searching for a very lightweight framework
I recently started using Masterpages, the thing is I would like to add text
I recently started using NERDTree with vim, and have set it to automatically open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.