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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:09:25+00:00 2026-05-28T05:09:25+00:00

I’m writing a generic wrapper class for a bunch of classes we have defined

  • 0

I’m writing a generic wrapper class for a bunch of classes we have defined in other code. I’m trying to figure out if there is a way in C++ to write a templated class that can store a template parameter as a variable. Here is an example of what I would like to see work:

template < class C > class generic {
    public:
        C obj; // used for custom type processing

        template < class T > T other; // <- changeable variable (or something)

        template < class T > setOther() { // Function to change variable
            // code to change variable type, or set type
        }

        void doSomethingWithOther() {
            // do some processing for other, like call
            // specific member function or something
            (((other*)obj)->*SomeGenericMethod)()
        }
}

int main(int argc, char **argv) {
    Bar bObj;
    generic<Foo> fObj;
    fObj.setOther<Bar>();
    fObj.doSomethingWithOther();
    return 0;
}

I know the above code won’t compile for many reasons, but my question is more in regards to functionality. Is there some way that I could store a template parameter as a variable (or use a function or something) that could then be used later as a type specifier? If there is not a standard way, any ideas from the SO community that might lead to my desired results?

Please no Boost or other 3rd party libraries, I’m trying to keep it as “STL” (C99) as possible.

Another idea I had was to use default specializations:

template < class C, class T = void* > class generic {
    public:
        C obj;
        T other;
        // ... more code
}

but in that instance, assuming we had the following:

class Test1 {
    public:
        generic<Foo> *G1;
        generic<Foo, Bar> *G2;
};

If I wanted to change the type of G1 later to do some other processing, I could say something like the following:

G1 = new generic<Foo, Bar>(); // compile error, different types

However, this would result in a compile error since the two types pointed to are of different types (G1 is of type generic< C > the other is of type generic< C, T > even though template class T is defaulted to void*.

Sorry for the long post, I am trying to be as clear as possible in my intentions with this code. As well, please feel free to correct me in any of my statements above (long days=less brain activity for me)

Thanks in advance for any help in this matter.

Edit 1:

To clarify, the other data member is merely there to show that I would like that member to hold a class type. This is because our code has members of other classes calling instanced member functions. And if I were to make a call to a member function of a different class I will get a compile error.

And unfortunately I cannot use any C++0x features as our embedded compilers are older and do not support the 0x feature set. Otherwise that would be the obvious solution.

I am essentially having to build in a delegate type of system so that instanced classes can call members of other instanced classes from within themselves and that called member function will have the memory space and access to the this pointer

Example:

class Foo {
    public:
        Delegate other;
}

class Bar {
    public:
        void SomeFunction() {
            std::cout << "hello from Bar::SomeFunction, data = " << data << std::endl;
        }

        int data;
}

int main() {
     Foo F;
     Bar B;
     B.data = 10;
     F.other = Delegate::attach<Bar>(&Bar::SomeFunction, &B);
     // Delegate::attach is defined as 
     // template < class T > Delegate attach(void (T::*fnPtr)(void*), T *obj)
     F.other();
}

In the above example void operator()() of the Delegate class then calls the other members function, via (*fnPtr)(). If called this way, the function has no access to it’s member data (no knowledge of the this pointer). If called passing the referenced object (i.e. (*fnPtr)(obj)), the function now has access to the member data.

I’m trying to solve this via specifying a generic class type object that could then be casted back and forth as the Delegate other may attach to any member.

  • 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-28T05:09:25+00:00Added an answer on May 28, 2026 at 5:09 am

    It’s not clear what the problem is that you are trying to solve. In your code, what is the purpose of class C? Anyway, here’s a guess at what you want. Use an underlying template class implementation that derives from a common base class with a virtual interface, and have the generic class hold a pointer to that interface.

    class generic {
        class interface {
        public:
            virtual ~interface(){}
            virtual void SomeGenericMethod()=0;
        };
        template <class T>
        class implementation: public interface {
            T data;
        public:
            implementation(const T& d) :data(d) {}
            virtual void SomeGenericMethod() 
            {return data.SomeGenericMethod();}
        };
        public:
            std::unique_ptr<interface> data;
    
            template < class T > void setOther(const T& other) {
                data.reset(new implementation<T>(other));
            }
    
            void doSomethingWithOther() {
                data->SomeGenericMethod();
            }
    };
    
    int main(int argc, char **argv) {
        generic fObj;
        Bar bObj;
        fObj.setOther(bObj);
        fObj.doSomethingWithOther();
        return 0;
    }
    

    http://ideone.com/A1Aqu

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.