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 1083029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:22:47+00:00 2026-05-16T22:22:47+00:00

I have two classes (ClassA and ClassB) who both have two methods (compare and

  • 0

I have two classes (ClassA and ClassB) who both have two methods (compare and converge). These methods work exactly the same way, but these classes are not related polymorphically (for good reason). I would like to define a function template that both of these classes can explicitly instantiate as a member but I’m getting errors because the methods use “this” and when I turn them into a template the compiler throws an error because they’re not member functions.

Is this impossible because of that limitation? Or is there some way to use “this” inside of a function template that is not declared as part of a template class. I’ve done some research and found nothing.

Logic.h

template <class T>
T* compare(const T& t) {
//stuff involving this
}

template <class T>
T* converge(const T& t,bool b) {
//other stuff involving this
}

ClassA.cpp

#include "ClassA.h"
#include "Logic.h"
//constructors

template ClassA* ClassA::compare(const ClassA& t) const; 
template ClassA* ClassA::converge(const ClassA& t,bool b) const;
//other methods

classB is similar.

Any help is appreciated!

  • 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-16T22:22:47+00:00Added an answer on May 16, 2026 at 10:22 pm

    I believe you can use CRTP here. Here is an example, you can omit the friend declaration in case you can do the compare using only public members:

    template<class T>
    class comparer
    {
    public:
        T* compare(const T& t)
        {
            //Use this pointer
            bool  b =  static_cast<T*>(this)->m_b  == t.m_b;
            return NULL;
        }
    };
    
    class A : public comparer<A>
    {
    public:
        friend class comparer<A>;
        A() : m_b(0)
        {
        }
    
    private:
        int m_b;
    };
    
    class B : public comparer<B>
    {
    public:
        friend class comparer<B>;
        B() : m_b(0)
        {
        }
    
    private:
        int m_b;
    };
    
    int main()
    {
        A a1,a2;
        A* p = a1.compare(a2);
    
        B b1,b2;
        B* p1 = b1.compare(b2);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two classes: public class Question { public IList<Answer> Answers { get;
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have two classes, and want to include a static instance of one class
I have two classes: Action and MyAction . The latter is declared as: class
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two Java classes: B, which extends another class A, as follows :
Ok I have two modules, each containing a class, the problem is their classes
Let's say I have two models, Classes and People. A Class might have one
I have two classes that each need an instance of each other to function.
I have two classes: Media and Container. I have two lists List<Media> and List<Container>

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.