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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:58:50+00:00 2026-05-24T21:58:50+00:00

There is a forward C struct declared in an unmodifiable header. I would like

  • 0

There is a forward C struct declared in an unmodifiable header. I would like to “virtually” add convenience member functions to it. Obviously my first choice would be to extend the struct and add the methods to the derived class. No-can-do, as the struct itself is declared as “forward” in the header, so I get the error “error: invalid use of incomplete type …”. I get a similar error if I try to define a new struct with a single element of the old struct. This sucks.

However, I was thinking that I could do some hackery with reinterpret_cast to get it to work anyway. The way it would go is this:

//defined in header
struct A forward;
void do_something_with_A(A* a, int arg);

//defined in my wrapper
struct B {
  B* wrap(A* a) {return reinterpret_cast<B*>(a); }
  void do_something(int arg) {do_something_with_A(reinterpret_cast<A*>(this),arg); }
}

If I added implicit conversions from type B to type A, I was thinking that this could work out almost as if B was a zero-data inheritor of A. However, this obviously brings up the question: is this undefined in C++? Normally I would think that accessing an element of an illegally casted struct would be undefined; that makes sense. However, I would think that reinterpret_casting from one type to another, passing that pointer around, and then casting back again, without doing anything illegal in between would be fine. I would also think that the way a compiler implements non-virtual struct members would be creating a function

B::do_something(B* b, int arg)

and calling that with the appropriate argument for B. This then reduces to the previous case, which by my dubious logic is okay. So I would think calling .do_something on a struct which is actually a reinterpret_cast A would be okay.

However, this says nothing for what the C++ standard actually says on the matter. Any help with that? Also, if someone has information on how well this will work practically, (i.e. “Every compiler ever made accepts this”, or “this only works with a few compilers”) that would also be helpful, but slightly less so.

  • 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-24T21:58:53+00:00Added an answer on May 24, 2026 at 9:58 pm

    I don’t believe this works if you’re using static_cast because you cannot static_cast between two completely unrelated class types. To be specific, if you have a pointer of type A* and try to convert it to a pointer of type B*, the static_cast only succeeds if this declaration is valid:

    B* ptr(myAPtr);
    

    or if B is non-virtually derived from A (which it isn’t). See the ISO spec §5.2.9 for details on the specifics of this. If we consider the above declaration, the only possible conversions that could be applied here in all of §4 are those in §4.10, and of those the only one that might be applicable is conversion from base to derived classes (§4.10/3), but this doesn’t apply here because A and B aren’t related types.

    The only cast you might be able to use here is a reinterpret_cast, and it doesn’t look like this will work either. In particular, the behavior of casting across class hierarchies is (§5.2.10/7)

    A pointer to an object can be explicitly converted to a pointer to an object of different type.65) Except that converting an rvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value, the result of such a pointer conversion is unspecified.

    So immediately there’s no guarantee that anything is going to work if the two objects have different alignment restrictions, and you cannot ensure that this is true. But suppose that you could. In that case, though, I believe that this will actually work correctly! Here’s the reasoning. When you call the member function of the B object, then rule &5.2.2/1) kicks in and says that, since the function is nonvirtual:

    […] The function called in a member function call is normally selected according to the static type of the object expression. […]

    Okay, so we’re at least calling the right function. Now, what about the this pointer? Well, according to &5.2.2/4:

    […] If the function is a nonstatic member function, the “this” parameter of the function (9.3.2) shall be initialized with a pointer to the object of the call, converted as if by an explicit type conversion (5.4). […]

    The type conversion done in the last part is the identity conversion from a B* to a B*, since that’s the selected type. So you’ve called the right function with the this pointer set appropriately. Nice! Finally, when you do a reinterpret_cast back to the original type, by the earlier rule, you’ll get back the A* object and everything will go as expected.

    Of course, this only works if the objects have the same alignment requirements, and this cannot be guaranteed. Consequently, you shouldn’t do it!

    Hope this helps!

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

Sidebar

Related Questions

Is there a straight forward way to view the SQL command text actually executed
Is there a way to forward-declare the HINSTANCE type from the WinAPI without including
Is there an easy and straight-forward method to select elements based on their data
Is there a possible htaccess directive that can transparently forward request from index.php to
I am using servlet there is two method redirect and forward both are send
Is there a trivial, or at least moderately straight-forward way to generate territory maps
Is there a pure-Java equivalent to <jsp:forward page=... /> that I can use within
The architecture of the application is straight forward. There is a web application which
There is a Flink and Blink (Forward Link and Backward Link) for a double-linked
I have the following for www.domain.com and login.domain.com . But spammers forward there site

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.