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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:24:57+00:00 2026-05-12T06:24:57+00:00

In my continuing adventure with templates, I’ve templated my Container class not just on

  • 0

In my continuing adventure with templates, I’ve templated my Container class not just on the ItemType it holds, but also on a Functor argument that determines how it should order the items. So far, so good.

A little problem I’ve run into occurs when I want to copy the contents of one Container to another: If the two Containers have different Functor types, then they technically are unrelated classes. Therefore, Container A isn’t allowed to access the non-public contents of Container B. Is there any good way to deal with this problem, other than making everything I need to access public? Some way to template a “friend” declaration, perhaps?

Example code to demonstrate the problem follows:

#include <stdio.h>

class FunctorA {};
class FunctorB {};

template <class ItemType, class Functor> class MyContainer
{
public:
   MyContainer() : _metaData(0) {/* empty */}

   template<class RHSFunctor> void CopyFrom(const MyContainer<ItemType, RHSFunctor> & copyFrom)
   {
      _metaData = copyFrom._metaData;
      _item     = copyFrom._item;
   }

private:
  int _metaData;
  ItemType _item;
};

int main(int argc, char ** argv)
{
   MyContainer<void *, FunctorA> containerA;
   MyContainer<void *, FunctorB> containerB;

   containerA.CopyFrom(containerB);  // error, containerA::CopyFrom() can't access containerB's private data!
   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-12T06:24:58+00:00Added an answer on May 12, 2026 at 6:24 am

    You can make a base template class templated just on ItemType, keep the data there, have the full-fledged 2-args template subclass that base, AND put the copy-from in the base class as it doesn’t depend on the functor anyway. I.e.:

    template <class ItemType> class MyContainerBase
    {
    public:
       MyContainerBase() : _metaData(0) {/* empty */}
    
       void CopyFrom(const MyContainerBase<ItemType> & copyFrom)
       {
          _metaData = copyFrom._metaData;
          _item     = copyFrom._item;
       }
    
    protected:
      int _metaData;
      ItemType _item;
    };
    
    template <class ItemType, class Functor> class MyContainer:
        public MyContainerBase<ItemType>
    {
      // whatever you need here -- I made the data above protected
      // just on the assumption you may need to access it here;-)
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing on my attempt to create a DateTime class , I am trying to
I'm continuing a previous post , but I thought I'd open a new thread
Continuing the discussion from Understanding VS2010 C# parallel profiling results but more to the
Continuing my journey into the world of variadic templates , I encountered another problem.
I am continuing work on my DirectShow application and am just putting the finishing
Continuing on this problem , but I'll reiterate: For a homework assignment I have
Continuing another but similar question about testing ( see here ). I'll use a
Continuing quest to make sense of ContT and friends. Please consider the (absurd but
Continuing this question. My problem is that I can not edit my JTable. I
Continuing from my last question C++ template class map I have implemented the function

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.