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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:33:56+00:00 2026-06-18T02:33:56+00:00

I’m writing a wrapper around some autogenerated classes to provide a better interface. I’ve

  • 0

I’m writing a wrapper around some autogenerated classes to provide a better interface. I’ve written a simplified example to demonstrate the problem.

I have a class A which has a pointer to an object of class X, then I have class B (inherits A) which has a pointer to an object of class Y (inherits X).

That’s all fine, except that I’m unsure what the best way is to store the pointers to X and Y. I need to be able to manipulate collections of objects of class A and B as if they were all A (with pointers to X being all I’d need there), which works fine with my example below.

My actual question is how best to handle the case of class B which needs to manipulate class Y objects rather than class X. I’m sure there is something quite obvious that I’m missing which I could use here. It feels clumsy to have to repeatedly cast this variable every time I use it and define a method with a different name (e.g. getY) for each class that inherits A that I create. There might be many methods like doStuff all of which would have to be calling getY.

class X {
private:
    int i;
public:
    X(int i) : i(i) {}
    int getI() {
        return i;
    }
};

class Y : public X {
private:
    int j;
public:
    Y(int i, int j) : X(i), j(j) {}
    int getJ() {
        return j;
    }
};

class A {
protected:
    X* x;
public:
    A(X* a) : x(a) {}
    X* get() {
        return x;
    }
};

class B : public A {
public:
    B(Y* y) : A(y) {}
private:
    //I could create a new function for each class like class B
    Y* getY() {
        return (Y*)x;
    }
public:
    void doStuff() {
        Y* y = getY();
        y->getJ();
        std::cout << "J: " << y->getJ() << cout::endl;
    }
};

Extract from main:

X* x = new X(5);
Y* y = new Y(5,6);
B b(y);
b.doStuff();

One alternative which occurs to me is that when I construct class B I could initialise a pointer of type Y variable pointing at the same location as the one which be set in X. As long as my pointers were const I think that should be safe from the two references ever pointing to different things.

Any ideas most welcome.

Thanks.

  • 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-06-18T02:33:58+00:00Added an answer on June 18, 2026 at 2:33 am

    Well, the first thing I would do is break the inheritance relationship between A and B. You say they have a common interface, so start by defining this interface:

    class ILibraryWrapper {
    public:
        virtual X * get() = 0;
    };
    

    I assume that A and B share a lot of code (hence the inheritence relationship), so next define a template class to implement the interface and define the common bits:

    template <class T> class LibraryWrapper : public ILibraryWrapper {
    protected:
        T * pointer;
    
    public:
        LibraryWrapper( T * value ) : pointer( value ) {}
    
        virtual X * get() override { return static_cast<X *>( this->pointer ); }
    };
    

    Finally, define A and B like so:

    class A : public LibraryWrapper<X> {
    public:
        A( X * value ) : LibraryWrapper( value ) {}
    };
    
    class B : public LibraryWrapper<Y> {
    public:
        B( Y * value ) : LibraryWrapper( value ) {}
    
        void doStuff() {
            std::cout << "J: " << this->pointer->getJ() << cout::endl;
        }
    };
    

    Virtually all your code should then deal with ILibraryWrapper pointers or references. If some code absolutely must know the concrete type of the wrapped object, then you can do a dynamic cast:

    auto wrapper = dynamic_cast<LibraryWrapper<Y> *>( object );
    if( wrapper != nullptr ) {
        // ...
    }
    

    But generally speaking, it’s better to provide all needed functionality in the ILibraryWrapper interface.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
i got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.