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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:34:03+00:00 2026-06-17T18:34:03+00:00

I have class A with basic members and functions: class A{ public: typedef struct{

  • 0

I have class A with basic members and functions:

class A{
public:
    typedef struct{
        int aa;
        int bb;
    } stEntry;
    stEntry entry;

    void function1();
    void function2();
};

Than class B that should extend class A including structure stEntry…

class B : public A{
public:
    typedef struct :stEntry
    {
        int cc;
    } stEntry;
    stEntry entry;

    void function3();
};

and then:

int main() {
    A a;
    B b;
    a.entry.aa = 1;    
    b.entry.aa = 2;
    b.entry.cc = 3;

    cout << "class A:"<<sizeof(a)<< endl;
    cout << "class B:"<<sizeof(b)<< endl;

    return 0;
}

I get

class A:8
class B:20

So class B contains 2 instances – 8 bytes(A class member) + 12 bytes(B class member).

Is there a way how to extend structure stEntry for class B? (without have 2 instances)

  • 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-17T18:34:04+00:00Added an answer on June 17, 2026 at 6:34 pm

    Sort of, with virtual inheritance:

    struct stEntryBase {
        int aa;
        int bb;
    };
    
    struct A : virtual stEntryBase {
        typedef stEntryBase stEntry;
    
        void function1();
        void function2();
    };
    
    struct stEntryDerived : virtual stEntryBase {
        int cc;
    };
    
    struct B : A, stEntryDerived {
        typedef stEntryDerived stEntry;
    
        void function3();
    };
    

    If you want to go another level of inheritance then B would derived virtually from stEntryDerived.

    Now you have to refer to the fields as a.aa, b.cc, there is no member entry. Also, the stEntry types are no longer POD (so bb and cc may no longer be adjacent in memory). Finally, the size increase due to the virtual inheritance might actually be bigger than two ints.

    What you can do, is get an stEntryDerived* or stEntryDerived& from an instance of B. Then that pointer/reference can be used to access aa, bb, and cc as the members of stEntryDerived, without the user needing to know about B. So you’ve achieved separation of interface, at some cost.

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

Sidebar

Related Questions

Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
I have a basic class that derived subclasses inherit from, it carries the basic
the basic idea is that you have some class that has a reference type
I have a library which has a basic class which is used extensively by
Consider I have a game-creation framework, with a basic class called Game. // Action
I have a basic Window class that I need to have events such as
I have a very basic data class that is subclassed from NSObject. I declare
basic c++ question i'm fairly sure. if i have a base class with a
I have a basic Django model like: class Business(models.Model): name = models.CharField(max_length=200, unique=True) email
i have a really basic java program just one .java/.class file (.java uncompiled .class

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.