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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:41:39+00:00 2026-05-23T13:41:39+00:00

struct A { B b; A(int x):b(x){} A(int x, float g) // how to

  • 0
struct A {
    B b;
    A(int x):b(x){}
    A(int x, float g) // how to implement this? I want to init b as a C. 
};
struct B {
    enum {e_c, e_d} type;
    int i;
    B(int i_):i(i_){}
};
struct C : public B {
    float f;
    C(int i_, float f_):B(i),f(f_){}
};
struct D : public B {
    double ff;
    D(int i_, double d):B(i),ff(d){}
};

Perhaps there is another way to code this? Originally I just had the class B, but I decided to split it up so I didn’t keep adding (incompatible/mutually exclusive) fields to B. My solution was, when A uses b it checks b’s enum to see what type it is, then casts a B* to a C* or a D* to get the float or double. My problem is now I don’t know how to let A initialize b. Does the language even let me do something like that?

edit: I just realized A’s b couldn’t possibly have allocated space to allow for C or D’s extra fields. There wouldn’t be any available space to store the float or double. So I guess the right way to do what i want is to add into B a union {float f; double ff;};?

  • 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-23T13:41:40+00:00Added an answer on May 23, 2026 at 1:41 pm

    Your example is a bit confused. But maybe you want a pointer?

    struct A {
        B *b;
        A(int x):b(new B(x)) {}
        A(int x, float g):b(new C(x,g)) {}
       ~A() { delete b; }  // Very important!
    };
    

    Note that if you do this, B must have a virtual destructor. You will also need to think about what it means to copy an A object.

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

Sidebar

Related Questions

After coming up against this problem myself in trying to implement a generic Vector2<int/float/double>
I have this code struct Student { char name[48]; float grade; int marks[10,5]; char
I have a struct like this: typedef struct { int sizes[3]; float **vals[3]; //
I have my struct: struct a { int x; float f; double d; char
#include<stdio.h> #include<conio.h> union abc { int a; int x; float g; }; struct pqr
I am curious why this code works: typedef struct test_struct { int id; }
Let's say I have a first structure like this: typedef struct { int ivalue;
struct SomeStruct { int a; int b; }; SomeStruct someFn( int init ) {
struct Div { int i; int j; }; class A { public: A(); Div&
I have the following struct: struct Records { int Number; char Name[20]; float Salary;

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.