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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:14:38+00:00 2026-05-23T09:14:38+00:00

As my prveious question sounded confusing, I think it’s best to clearly state what

  • 0

As my prveious question sounded confusing, I think it’s best to clearly state what I want to achieve.

I have (ignore the inheritance for now and focus on X):

class Base {};

class X : public Base {
private:
    double m_double;
public:
    template<class A> friend 
    void state( A& a, const X& x ) {
        data( a, x.m_double, "m_double" );
    }   
};

I can now introduce arbitrary new classes that performs different actions based on how the data function is overloaded, we will refer to these as Accessors in the following:

class XmlArchive {...}; //One Accessor
template<class T>
void data( XmlArchive& a, const T& t, const std::string& str ) {
//read data and serialize it to xml Archive
}

class ParameterList {...}; //Another Accessor
template<class T>
void data( ParameterList& a, const T& t, const std::string& str ) {
//put data in a parameter list 
}

I can then write:

X myX;

XmlArchive myArchive;
state( myArchive, myX );

ParameterList myParameters;
state( myParameters, myX );

Fantastic, code reuse! 😀 However the following (clearly) fails:

Base* basePtr = new X; //This would come from factory really, I should not know the type X
state( myParameters, *basePtr ); //Error

The goal is to make this last call succed. What I have considered (and why it is not acceptable):

First option: make all Accessors inherit from a common base class, say AccessorBase, then write in Base

virtual state( AccessorBase& a ) const = 0;

and implement the required code in X (the syntax to call state will be marginally different but this can be fixed).
The problem is that AccessorBase will need to have a virtual function for every possible type which comes as second argument in the data function(s). As these types can be user-defined classes (see case of class composition, X which has Y as data member) I do not see how to make this strategy can work.

Second option: create a virtual function in Base for every Accessor. This violates the open/close principle as adding a new Accessor class (say TxtArchive) will require modification of the base class.

I understand why virtual member function cannot be templated (possible different vtbls in different compilation units).
However it seemes to me that there should be a solution to this problem… Base knows that it really is of type X, and the type of the Accessor is always explicit, so it is a matter of finding a way of calling (for Accessor of type XmlArchive):

state( xmlArchive, x ); //xmlArchive of type XmlArchive, x of type X

which will yield the result.

To sum-up I would like the call:

state( myParameters, *basePtr );

to succeed if basePtr is pointing to a derived class with a function template compatible with the call and to throw an exception otherwise.

It seemes to me that boost::serialize does something similar but I cannot figure out how (it may be it is re-implemnting inheritance relationships in C++ via templates, I see a This() funcion returning the most derived pointer but it gets really confusing…)

Thank you again in advance for your help!

  • 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-23T09:14:39+00:00Added an answer on May 23, 2026 at 9:14 am

    You could use the vistor pattern for this:

    class IVisitor
    {
      public:
        virtual void on_data( double, const char * )=0;
        virtual void on_data( std::string, const char * )=0;
    }; 
    
    class Base 
    {
      public:
        virtual void visit( IVisitor * v )=0;
    };
    
    class X : public Base 
    {
      private:
        double m_double;
        std::string m_string;
      public:
        void visit( IVisitor * v)
        {
            v->on_data( m_double, "m_double" );
            v->on_data( m_string, "m_string" );
        }   
    };
    
    Base * base = ...;
    XmlArchive ar;  // This must derive from IVisitor
    base->visit(&ar);
    ParameterList pl; // This must derive from IVisitor
    base->visit(pl);
    

    If you dislike the need to implement each of the different on_data types in the IVisitor, you can use a boost::any – but in my experience the branching required in the implementations of that function are not worth it.

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

Sidebar

Related Questions

This question is related to my previous question . The storyline: I have an
This question ties to my previous question but is more specific. Say I have
After my previous question I have managed to Add and Ad banner to my
Link to my previous question , I want to rewrite the http://www.demo.com/context/user.do?action=home to http://www.demo.com
Following on from a previous question . Lets say I have two checkboxes on
My previous question belied my inexperience and was based on an assumption. Now I
(This question is loosely related to my previous question ) Hello there. I have
Moving ahead from previous question .I have two rectangle and they look like this:
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question is directly related to my previous question ASP.NET AJAX Is it possible

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.