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

  • Home
  • SEARCH
  • 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 8083711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:33:47+00:00 2026-06-05T17:33:47+00:00

I have a structure like this struct structure { BaseObject &A; //BaseObject has a

  • 0

I have a structure like this

struct structure
{
    BaseObject &A; //BaseObject has a function declared as virtual
};

In run time, I am dynamically assign an object to &A

structure *s = new structure;
DerivedObjectB *B = new DerivedObjectB(); //Derived class overloads the virtual function 
s->A = *B; //s is a pointer of the structure. S has been initialized 

I can compile this code but I am getting a seg-fault error in runtime.
I have a restriction that I can not use a pointer.
This is not a homework. The compiler I am using as a reverse compiler has a restriction of using pointers because of an issue in building SSA

  • 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-05T17:33:49+00:00Added an answer on June 5, 2026 at 5:33 pm

    If you cannot use a pointer, you must use a reference.
    If you use a reference, it must be initialized to it’s final value as it’s constructed.

    struct structure
    {
        BaseObject &A; //BaseObject has a function declared as virtual
        structure(BaseObject &A_) : A(A_) {}
    };
    
    DerivedObjectB *B = new DerivedObjectB(); 
    structure *s = new structure(*B); //done
    //keep in mind you cannot delete `B` until _after_ you delete `s`
    

    Your code above shouldn’t have compiled since a structure could not be created that way since it had no automatic default structure since it has a reference member. Also, Even if it did compile, the A member would have been a BaseObject copy of the DerivedObjectB‘s parent BaseObject object, or some other bizzare not-what-you-wanted.

    Are you absolutely sure you cannot use pointers there? That makes no sense at all, and makes this very difficult to work with.

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

Sidebar

Related Questions

I have structure for storing callback function like this: template<class T> struct CommandGlobal :
I have program, that has structure defined like this: struct foo { int magic;
Let's say I have a first structure like this: typedef struct { int ivalue;
I have a large number of instances of a C structure like this: struct
The typical Python threadpool will have a structure like this one: def run(self): while
I have structure like this below. Now, I want swap 2 structures. public struct
I have a structure something like this struct MyStructure :IFoo { } and a
I have a C structure like this : typedef struct ip_esp_private { /* keep
I have seen structure declarations which looks like this one typedef struct br {
I have a List of an Record(structure) like this : struct Rec { int

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.