-1

My intention is to store a list of B objects in class A, but I want a new element to be created in A list when I call B constructor.

I have a code like this:

class A
{...
    protected:
      std::list<B> Blist;
      std::list<B>::iterator Bit;
 ...
    public:
      Update();
 ...
    friend class B;
}


class B
{...
    protected:
       A* p_A;
 ...
    public:
       B(); //Standard constructor
       B(A* pa); // This is the constructor I normally use
}


B::B(A* pa)
{
    p_A=pa; // p_A Initialization

    p_A->Bit = p_A->Blist.insert(p_A->Blist.end(), *this);
}

A::Update()
{

   for(Bit=Blist.begin(); Bit != Blist.end(); Bit++)
   {
     (*Bit).Draw() //Unrelated code
   }

}

void main() //For the sake of clarity
{

    A* Aclass = new A;
    B* Bclass = new B(A);

    Aclass.Update(); // Here is where something goes wrong; current elements on the list are zeroed and data missed

}

Well, the program compile with no difficulty, but when I run the program I don't get the desired result.

For B I have two constructors, a default one which zeroize everything and another which accepts inputs to initialize internal variables.

When I use the second to initialize private variables, then during A.Update method, everything is zeroed and looks like I would have used default constructor instead.

Am I doing something wrong? Is my approach correct?

Thanks you!

EDIT: PROGRAM EDITED FOR CLARITY

Adrián
  • 11
  • 2
  • Where do you init/assign a (new) `A` object? Currently you just have a void/null pointer in `p_A->Bit` – Daan Timmer Sep 06 '12 at 22:42
  • Also, why are you using `std::list` and not `std::list<*B>`. ? I get the feeling you don't really know what you are doing at all? – Daan Timmer Sep 06 '12 at 22:47
  • @DaanTimmer What is wrong with `std::list` ?? – mathematician1975 Sep 06 '12 at 22:49
  • @mathematician1975 nothing. However he will run in to problems in the `A::update` method if it will do more than just call `.Draw();` For example when the `B` object changes his internal values. He is currently only copying the B object in to the list. It will also double the memory usage etc. etc. – Daan Timmer Sep 06 '12 at 22:52
  • Potential recursion problem here - a `B` item is created, telling an `A` to add a `B` to it's list, creating another `B`, which tells the `A` to add another... and so on – tmpearce Sep 06 '12 at 22:54
  • @DaanTimmer, you got it! Switching object list to pointer list worked the problem out. Thank you. But please, there is no need to be so rude - yes i'am a beginner, that's why I'm here making question instead of solving them – Adrián Sep 07 '12 at 00:13
  • You definitely have credit for trying, which is considerably more than many others. Your question wasn't "this is the problem; gimme the codez" all-too-often seen on SO. Anyway, glad you found help. Good luck with your studies, and don't give up. – WhozCraig Sep 07 '12 at 02:54
  • @Adrián my excuses if I sounded rude. Wasn't meant to be. I shall answer to question for you to accept (if you want to) – Daan Timmer Sep 07 '12 at 09:55

3 Answers3

2

You may want to try initializing your p_A before dereferencing it.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Initialize p_A is not the problem - it's passed as an argument in the original code. I'm going to edit previous code to make it clearer – Adrián Sep 06 '12 at 23:31
1
std::list<B> Blist;

This is a list of objects of type B. When you insert(iterator,value), you're giving the list a value to copy. This generates a new B object to be held by the list, which is created by the copy constructor. If B's copy ctor doesn't do the initialization steps you require, the object won't be in the state you expect.

std::list<B*> Blist;

Keeping a list of pointers instead of objects will let the A object access the B item that was already created, rather than creating a new B object that lives in the list.

tmpearce
  • 12,523
  • 4
  • 42
  • 60
  • This is considerably more deserving of the correct answer rather than mine now that the code has been fixed and documented to have p_A set elsewhere. @tmpearce is correct concerning the valuation vs. reference of B. It could be considerably improved by defining proper copy constructors where appropriate, and removing default constructs where unneeded, but that is more refinement than anything else. – WhozCraig Sep 07 '12 at 02:48
0

Changing:

std::list<B> Blist;
std::list<B>::iterator Bit;

to

std::list<B*> Blist;
std::list<B*>::iterator Bit;

and

p_A->Bit = p_A->Blist.insert(p_A->Blist.end(), *this);

to

p_A->Bit = p_A->Blist.insert(p_A->Blist.end(), this);

Should solve your problem.

Daan Timmer
  • 14,771
  • 6
  • 34
  • 66