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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:56:02+00:00 2026-06-16T17:56:02+00:00

Suppose there are two struct A and B. They have a common struct C.

  • 0

Suppose there are two struct A and B. They have a common struct C.
I would like to know if it is safe to call reinterpret_cast to A or B to C.
if not, is there any way to do so without any performance impact?

struct C
{
    string m_c1;
    int32_t m_c2;
    double m_c3;
    string m_c4;
};

struct A
{
    C m_a1;
    string m_a2;
    int32_t m_a3;
};

struct B
{
    C m_b1;
    string m_b2;
    int32_t m_b3;
    double m_b4;
};
int main(int argc,char *argv[])
{
        A a;
        a.m_a1.m_c1="A";
        a.m_a1.m_c4="AA";

        B b;
        b.m_b1.m_c1="B";
        b.m_b1.m_c4="BB";

        C* pc = reinterpret_cast<C*>(&a);
        cout << pc->m_c1 << " " << pc->m_c4 << endl;

        pc = reinterpret_cast<C*>(&b);
        cout << pc->m_c1 << " " << pc->m_c4 << endl;

        return 1;
}
  • 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-16T17:56:04+00:00Added an answer on June 16, 2026 at 5:56 pm

    Why dont you inherit both A,B from C and then use static_cast instead? Should be safer/cleaner.

    Indeed in your case, you shouldnt need a cast at all, you should be able to assign A or B ptrs to a C*

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

Sidebar

Related Questions

Suppose i have one folder and inside that another two files are there .
Suppose you have two linked servers called Local and Remote respectively. Is there a
Suppose I have a string like this: one two three four five six seven
I have two classes university and department, suppose there is one to many relationship
Suppose there are only two type of model objects. Tag Article Article can have
Suppose I have two struct s: struct X {}; struct Y { X x;
Suppose we have two structs: typedef struct Struct1 { short a_short; int id; }
Suppose there are two lists a = {a1, a2, a3} and b = {b1,
Suppose that there're two types of packet in the chatting server. Those two packets
Suppose there is a situation like : statement 1; statement 2; statement 3; statement

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.