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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:17:36+00:00 2026-05-15T10:17:36+00:00

I have three structs, one of which inherits from the other two: typedef struct

  • 0

I have three structs, one of which inherits from the other two:

typedef struct _abc
{
    int A;
    int B; 
    int C;
} ABC;

typedef struct _def
{
    int D;
    int E;
    int F;
} DEF;

typedef struct _abcdef : ABC, DEF { } ABCDEF;

(I want the third struct to contain all the members of the first two. Is there a better way to do it?)

Now, I have fully populated instances of ABC and DEF, and I want to make a new ABCDEF instance:

int main()
{
    ABC abc;
    abc.B = 2;
    abc.C = 1;
    abc.A = 10;

    DEF def;
    def.D = 32;
    def.E = 31;
    def.F = 90;

    // make a new ABCDEF with all the values of abc and def
}

What is the best way to create the ABCDEF? I could do this, but it kinda sucks:

ABCDEF abcdef;
abcdef.A = abc.A;
abcdef.B = abc.B;
abcdef.C = abc.C;
abcdef.D = def.D;
abcdef.E = def.E;
abcdef.F = def.F;

The above is coupled to the members of the structs, and is messy if many members are added.

  • 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-15T10:17:37+00:00Added an answer on May 15, 2026 at 10:17 am

    This is C++. Those typedefs aren’t needed. And you have constructors:

    struct abc {
        int A;
        int B; 
        int C;
        abc(int a, int b, int c) : A(a), B(b), C(c) {}
    };
    
    struct def {
        int D;
        int E;
        int F;
        def(int d, int e, int f) : D(d), E(e), F(f) {}
    };
    
    struct abcdef : abc, def {
      abcdef(const abc& a, const def& d) : abc(a), def(d) {}
    };
    
    int main()
    {
       abc a(2,1,10);
       def d(32,31,90);
       abcdef ad(a,d);
    
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I need to have two lists, one of which keeps the list of
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
I have two structs like so: public struct KeyLog { Keys key; DateTime time;
I have a struct defined in a header file with three other files that
I have a problem. There are two classes: struct Base { Base* retain() {
I have three column in my datagridview .One is text ,one is Combo and
I have three computers on my LAN, one running ubuntu , one running openSuse
I have a set of classes which are all derived from a common base
I have a class which I want to expose a list of structs (which
I have data structure which stores POD-structs (each instantiation stores a single type only,

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.