Re: Circular dependency - I think..

From: ma740988 (ma740988_at_pegasus.cc.ucf.edu)
Date: 07/27/04


Date: 27 Jul 2004 04:24:00 -0700


[....]
> I see that you are not initialising the pointer to BAR contained in FOO.
I dont think I need to. If i do that I end up with the circular
dependency problem. In fact I'd get an exception if i initialize the
pointer to BAR contained in FOO. For simplicity.

// bar.h
#ifndef BAR_H
#define BAR_H
# include <memory>
# include "foo.h"

class FOO;
class BAR
{
public:
  BAR();
  ~BAR();
private:
  int in_use;
  FOO* foo;
};

//bar.cpp
# include "bar.h"
# include <iostream>

BAR::BAR() : in_use(0), foo(new FOO())
{
 std::cout << " bar's constructor " << std::endl;
}

BAR::~BAR()
{
  std::cout << " bar's destructor " << std::endl; delete foo;
}

// foo.h
#ifndef FOO_H
#define FOO_H

# include <memory>
# include "bar.h"

class BAR;
class FOO
{
public:
  FOO();
  ~FOO();

private:
  int idx;
  BAR* bar; // Initialize HOW??
};

#endif

//foo.cpp
# include<iostream>
# include "foo.h"

FOO::FOO() : idx(0)
{
  std::cout << " foo's constructor called " << std::endl;
}

FOO::~FOO()
{
  std::cout << " foo destructing " << std::endl;
}

// main_test.cpp
int main()
{
  BAR *ptrBar = new BAR;
  for (int Idx(0); Idx < 10; ++Idx)
    //ptrBar->GetPosFbkFoo();
  delete ptrBar;
}

[...]
>
> You have not initialised BAR's member auto_pointer. Instead, you have
> declared another auto_pointer as a local variable of the constructor. It
> goes out of scope --- and hence calls delete on the memory pointed to ---
> when the constructor finishes. Usually the best way to initialise the member
> variable is using the initialiser list:
>
> BAR::BAR() : in_use(0), foo( new FOO )
> {
> std::cout << " bar's constructor " << std::endl;
> }
Yes..

> If you want to initialise foo later than this, then you must use auto_ptr's
> reset member function.
>
Got it..

[....]
>
> Just by the way: I find it very confusing when no naming distinction is made
> between objects and pointers to objects --- especially when the names used
> are just lower case versions of the class names. I always make "p" the first
> letter in the name of any pointer. Thus I would use pfoo and pbar as the
> pointer names and use foo and bar as names of objects.

Point taken..



Relevant Pages

  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (comp.arch.embedded)
  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (sci.electronics.design)
  • Re: "Mastering C Pointers"....
    ... > Windows shortcut, there is definitely an analogy there. ... a pointer variable holds an address of an object. ... int foo = 12; ... int *bar; ...
    (comp.lang.c)
  • Re: Interview questions
    ... and note that this means the implementor of Bar needs to ... Bar might want to inherit some virtual methods of Foo. ... is the first member of bar, we can say a Bar "is a" Foo. ... > You cannot pass a pointer to an object of type Bar ...
    (comp.lang.cpp)
  • Re: Getting the instance pointer from a called function
    ... >> pointer) which called bar(), from inside bar, when that happens. ... store a pointer to the current instance of Foo in ... will store pointers of Foo instances, but barhas a strict prototype and ...
    (comp.lang.cpp)