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

  • SEARCH
  • Home
  • 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 6194787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:21:20+00:00 2026-05-24T03:21:20+00:00

I have a complex code base at work, and i created a small example

  • 0

I have a complex code base at work, and i created a small example to mimic the problem and here is the below code.

< Code below for reference> – This code is compilable if we have boost libraries and FastDelegate.h linked with the project. Please let me know if you need the full compilable example project, i can email you.

I have two problems and need help resolving them.

  1. As seen below in the code, i have a class with argument type as template for another classes object. Now when i initialize the class below in UserClass’s constructor (Line 107) i get error because mBaseAcceptor is a class with template argument of type base Class, but i need to do mbaseAcceptor(new derivedAcceptor_t). Casting problem how to fix this?

Error here is

./boost/smart_ptr/shared_ptr.hpp:387:9: error: comparison between distinct pointer types ‘Acceptor<DerivedClass>*’ and ‘Acceptor<BaseClass>*’ lacks a cast
  1. Another problem is in line 108, even if i magically say resolve this by using another acceptor of derived class, this is where i use that mDerivedAcceptor, in Line 108 i do

    mDerivedAcceptor->SetDelegate(fastdelegate::MakeDelegate(this, &UserClass::HandleDelegate)); 
    

then i get error saying

"error no matching function call for HandleDelegate(DerivedClass&, bool). 

This make sense because HandleDelegate has argument of type BaseClass and by storing a delegate(which is a func. ptr) we have to call the function with appropriate argument. But how to fix this.

  1. If i cast Handler inside Acceptor class with derived class will it work when i only pass the baseClass pointer?

Code

/*
 * smart_pointer_1.cpp
 *
 *  Created on: Jul 26, 2011
 *      Author: balaji
 */
#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include "FastDelegate.h"
#include <iostream>
using namespace std;

template <class Handler>

class Acceptor {

public:
    typedef fastdelegate::FastDelegate1<Handler &, bool> delegate_t;
    Acceptor ();
    void Initialize(Handler *&handle);
    void SetDelegate(delegate_t delegate) { mDelegate = delegate; }

private:
   int mValues[2];
    delegate_t mDelegate;
};

template <class Handler>
Acceptor<Handler>::Acceptor()
{
    std::cout << "In Constructor: " << __FUNCTION__ << std::endl;
    mValues[0] = 1;
    mValues[1] = 2;

}   

template <class Handler>
void Acceptor<Handler>::Initialize(Handler *&handle){
    if (!handle) {
        std::cout << __FUNCTION__ << " : created" << std::endl;
        handle = new Handler();
    } else {
        std::cout << __FUNCTION__ << " : Error exception" << std::endl;
    }
    if (mDelegate && mDelegate(*handle)) {
        std::cout << "Ok Called Handle in " << __FUNCTION__ << std::endl;
    } else {
        std::cout << "Not Called Handle in " << __FUNCTION__ << std::endl;
    }

   handle->displayComputer();
}

class BaseClass {
    std::string mComputer;
public:
    BaseClass() {
    std::cout << "In Base Constructor: " << __FUNCTION__ << std::endl;
    mComputer = "Mac";
    }
    virtual void displayComputer() {
    std::cout << "Computer type is " << mComputer << std::endl;
    }
};

class DerivedClass : public BaseClass {
    std::string mLanguage;
public:
    DerivedClass() {
    std::cout << "In Derived Constructor: " << __FUNCTION__ << std::endl;
    mLanguage = "C++";
    }
    void displayComputer() {
    std::cout << "Language is " << mLanguage << std::endl;
    }
};

class UserClass {
public:
    UserClass();
    UserClass(bool);
    typedef Acceptor<BaseClass> baseAcceptor_t;
    typedef Acceptor<DerivedClass> derivedAcceptor_t;
    typedef boost::shared_ptr<BaseClass> basePtr_t;
    void CallDelegate(BaseClass&);

private:
    boost::shared_ptr<baseAcceptor_t> mBaseAcceptor;
    boost::shared_ptr<derivedAcceptor_t> mDerivedAcceptor;
    BaseClass *mConnBasePtr;

    bool HandleDelegate(BaseClass& baseDelegate);
};

UserClass::UserClass() : mBaseAcceptor(new baseAcceptor_t)
{
    std::cout << "In Constructor: " << __FUNCTION__ << std::endl;
    mBaseAcceptor->SetDelegate(fastdelegate::MakeDelegate(this, &UserClass::HandleDelegate));
    mBaseAcceptor->Initialize(mConnBasePtr);
}

UserClass::UserClass(bool value)
{
    std::cout << "In Constructor: " << __FUNCTION__ << std::endl;
    mBaseAcceptor.reset(new derivedAcceptor_t);         //    <<========== Problem Here because of improper casting
    mBaseAcceptor->SetDelegate(fastdelegate::MakeDelegate(this, &UserClass::HandleDelegate));   //   <<=== Also here because of improper type passed to MakeDelegate function ptr. Please note HandleDelegate has an argument of type BaseClass, but Acceptor is derived class
    mBaseAcceptor->Initialize(mConnBasePtr);
}


bool UserClass::HandleDelegate(BaseClass& baseDelegate)
{
    std::cout << "In " << __FUNCTION__ << std::endl;
    return true;
}


int main() {
    std::cout << "In function: " << __FUNCTION__ << std::endl;
    typedef boost::shared_ptr<UserClass> userPtr_t;

    userPtr_t user(new UserClass(true));

    std::cout << "In function: " << __FUNCTION__ << " at end "<< std::endl;
    return 0;
}
  • 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-05-24T03:21:21+00:00Added an answer on May 24, 2026 at 3:21 am

    Define base class for the Acceptor template and another class that will be base to all Handler types. So your implementation will change to:

    class IHandler {
    };  
    
    class IAcceptor {
    public:
        virtual void Initialize(IHandler *) = 0;
        virtual void SetDelegate(delegate_t delegate) = 0;
    };
    

    Your Acceptor template will change to:

    template <class Handler>
    class Acceptor : public IAcceptor {
    public:
        typedef fastdelegate::FastDelegate1<Handler &, bool> delegate_t;
        Acceptor ();
        void Initialize(IHandler *pVal);
        void SetDelegate(delegate_t delegate) { mDelegate = delegate; }
    private:
       int mValues[2];
        delegate_t mDelegate;
    };
    

    Your implementation for Initialize will change (Make sure you handle the dynamic_cast result correctly):

    template <class Handler>
    void Acceptor<Handler>::Initialize(IHandler *pVal){
        Handler *pHandle = dynamic_cast<Handler>(pVal); //You will have to ofcourse ttake appropriate action if this cast fails.
        if (!handle) {
        std::cout << __FUNCTION__ << " : created" << std::endl;
        handle = new Handler();
        } else {
        std::cout << __FUNCTION__ << " : Error exception" << std::endl;
        }
        if (mDelegate && mDelegate(*handle)) {
        std::cout << "Ok Called Handle in " << __FUNCTION__ << std::endl;
        } else {
        std::cout << "Not Called Handle in " << __FUNCTION__ << std::endl;
        }
    
       handle->displayComputer();
    }
    

    Finally all classes that have to be used with the Acceptor will have to be derived from IHandler.

    Now you can change your pointer declaration to shared_ptr< IAcceptor >.

    EDIT:

    Based on your comment for the second issue, I would pass the Handler object as a pointer instead of a reference and modify the UserClass::HandleDelegate method to accept a pointer to the BaseClass (or the IHandler class if you want to be even more generic.).

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

Sidebar

Related Questions

I have this long and complex source code that uses a RNG with a
I have this piece of code: var myObj = function () { this.complex =
I have to buil a rather complex WPF UI (a hughe Grid) in code
I have this code that prints out columns in my DB and adds a
I've worked on projects that have very complex XML configuration, and one problem that's
I have a problem with the following code: class SymmetryTypes { public: enum Type
I have to work out a small framwork that is going to be used
I have a complex form for scheduling events. Here are the abbreviated associations: class
I have complex GUI application written in Python and wxPython. I want it to
I am using WCF and REST, and I have complex types, which are working

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.