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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:14:28+00:00 2026-06-15T09:14:28+00:00

I have a struct like this one: struct foo { IBar* ptr; }; which

  • 0

I have a struct like this one:

struct foo
{
    IBar* ptr;
};

which is part of a foo object with a long lifetime. Users often get instances of those objects in different callbacks and sometimes they inject stuff into the IBar* in certain places, use it in later callbacks, free it eventually. (IBar has virtual void free() { delete this; } which can be overridden of course).

The problem is I want to transition my users to the possibility of having more than one IBar* per foo struct and I want to make the transition smooth. One idea for making the transition smooth is changing the foo struct as follows:

struct foo
{
    foo()
    {
        ptr = reinterpret_cast<IBar*>(new IBar*[N]);
        memset(ptr, 0, sizeof(IBar*)*N);
    }

    IBar*& getBarPtr(size_t offset)
    {
        return reinterpret_cast<IBar**>(ptr)[offset];
    }

    IBar* ptr;
};

My idea is that this way, anyone using the old style where they expect there to be only one IBar* should fallback on using the first of the N transparently in the usual way of using it:

someFoo.ptr = new iImplementIBar(...);

but those users who begin to transition to new usage can get their offset and instead do

someFoo.getBarPtr(myOffset) = new iImplementIBar(...);

the problem, though, is that foo had a reset method which used to do this:

void reset() {
    if (ptr)
    {
        ptr->free();
        ptr = 0;
    }
}

which this idea replaces with this:

void reset() {
    IBar** ptrs = reinterpret_cast<IBar*>(ptr);
    for (size_t i = 0; i < N; ++i)
         if (ptrs[i])
        {
            ptrs[i]->free();
            ptrs[i] = 0;
        }
}

and the functionality of the free in the above seems to be lost. Is there a way to make this work? Or what goes wrong?

  • 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-15T09:14:30+00:00Added an answer on June 15, 2026 at 9:14 am

    If you really must do this without implementing a newer interface, why not have something like this:

    struct foo
    {
        IBar **bars; // or better yet: std::array or std::vector if size of N is fixed.
        IBar *ptr;
    
        IBar *getBar(int index)
        {
            ...
        }
    
        ...
    };
    

    That way users of the existing ‘foo’ interface continue to use ptr but the new bars interface is also available for those who wish to use it.

    Without knowing more it’s hard to know whether the above design makes sense.

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

Sidebar

Related Questions

For example, I have a struct which is something like this: struct Test {
Say i have a data structure like this: type Foo struct { Bar []struct
Ok so I have struct like this typedef struct { float x; float y;
I have a struct like this typedef struct bookStruct { char title[80]; char author[80];
If I have a struct like this: typedef struct { unsigned char c1; unsigned
Hi I have a struct like this typedef struct { string firstname; string lastname;
I have a C struct like this: typedef struct { my_domain_type_t type; my_domain_union_t u;
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
I have a struct that's defined like this: struct Vec3 { float x, y,
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {

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.