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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:40:33+00:00 2026-05-26T17:40:33+00:00

Say I have a class that can use different types of distance functions (Euclidean

  • 0

Say I have a class that can use different types of distance functions (Euclidean distance, etc) to do some comparisons. I have implemented these functions as subclasses of a class Distance:

class Distance;
class EuclideanDistance : public Distance;
class OtherDistance : public Distance;

It seems that to choose which type of distance to use, I could do:

template <typename TDistance>
class MyClass;

and instantiate it with:

MyClass<EuclideanDistance> instance;

or accomplish the same thing with:

class MyClass
{
public:
 Distance* myDistanceFunction;
}

and instantiating it with

MyClass instance;
instance.myDistanceFunction = new EuclideanDistance;

are there any advantages of one method over the other?

Thanks,

David

  • 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-26T17:40:34+00:00Added an answer on May 26, 2026 at 5:40 pm

    Association (i.e. the solution without templates) is preferable since it offers you more flexibility, allows you to change distance implementation at runtime, generates cleaner error messages and cleaner object files (fewer symbols).

    Also, classes generated from the template parametrized with different types (distance implementations) will be considered different types and will not be interchangeable: MyClass<EuclideanDistance> is a different type than MyClass<MinkowskiDistance>. This will force you to make all functions that operate on MyClass templates as well and will ultimately lead to greater complexity with no added benefits.

    Templates should be used when you need to relax type-safety of the language, for example when you are writing a class which should operate on a number of unrelated types (not derived from a common base class/interface) which nonetheless behave in a similar manner (e.g. all have a kwak() member function). This is called duck-typing: types are formally unrelated but all exhibit similar properties.

    In case you’re prevented from ensuring that all distance implementations derive from a common base class/interface, you may need to use templates. Otherwise, prefer simple and flexible association.

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

Sidebar

Related Questions

Say I have a class, that wraps some mathematic operation. Lets use a toy
Let me explain best with an example. Say you have node class that can
Let's say I have a class that requires configuration, dependency injection etc. public class
Let's say that I have class , that uses some functionality of dict .
I have a number of different types of content containers that I use in
Say I have these 3 different functions that I may want to point to:
Say I have a class that looks like this (get/set omited): class InfoClass{ String
Let's say we have a class that embed another parametric one, that has to
Lets say I have a class A that is fairly simple like this -
Say I have a class which represents a person, a variable within that class

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.