-2

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

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
  • 1
    That code cant compile, references are not assignable. Show us real code. – K-ballo Jun 12 '12 at 17:31
  • @K-ballo I disagree (about the compile part). – Luchian Grigore Jun 12 '12 at 17:32
  • "*I have a restriction that I can not use a pointer.*" Who would give you such a silly restriction? Is this homework? – ildjarn Jun 12 '12 at 17:33
  • We need more information. Where does `s` come from? – Cameron Jun 12 '12 at 17:33
  • 1
    @Luchian Grigore: True, it will compile but not do what the OP expects. – K-ballo Jun 12 '12 at 17:33
  • 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 – codereviewanskquestions Jun 12 '12 at 17:34
  • And `s` is a valid pointer that points to a struct with valid reference to a live object? :-) – Bo Persson Jun 12 '12 at 17:38
  • Might be relevant: http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c – anatolyg Jun 12 '12 at 17:47
  • 2
    The code is ill-formed because `structure` has no default constructor; the reference type data member inhibits the implicit declaration of a default constructor (in C++11, its implicitly declared default constructor is defined as deleted). – James McNellis Jun 12 '12 at 17:57
  • @James : If I'm not mistaken, `structure` is an aggregate type, so the following should work as-is: `BaseObject a; structure s = { a };`. – ildjarn Jun 12 '12 at 18:49
  • @ildjarn: Yes, aggregate initialization would work. `new structure` won't, though, because of the missing default constructor. – James McNellis Jun 12 '12 at 18:56
  • @James : Ah, I see what you mean. Somehow I thought you were referring to the definition of `structure` itself as ill-formed. Sorry for the noise. – ildjarn Jun 13 '12 at 00:46

4 Answers4

2

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.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
1

You cannot change object referenced by A, once structure is constructed. What you probably would like to have is:

struct structure
{
  structure() : A(NULL) {}
  ~structure() { if (this->A) { delete this->A; } }
  BaseObject * A;
};

structure *s = new structure;
s->A = new DerivedObjectB();

Anyway, using raw pointers is error prone and you should consider it.

Greg
  • 1,660
  • 13
  • 12
0

I'm betting you just declared

structure* s;

or

structure* s = NULL;

amirite? Otherwise, there's no reason for the crash.

Or s->A references an object that went out of scope, so when you call operator = on it you get into undefined behavior.

It's one of these two.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

This should not even compile. The reference cannot be rebound after construction, and you should have received an error telling you that it must have been bound to something.

Puppy
  • 144,682
  • 38
  • 256
  • 465