Re: Circular dependency - I think..
From: ma740988 (ma740988_at_pegasus.cc.ucf.edu)
Date: 07/27/04
- Next message: tom_usenet: "Re: is using tons of stl a correct method?"
- Previous message: gswork: "Re: Hello World..."
- In reply to: John Carson: "Re: Circular dependency - I think.."
- Next in thread: John Carson: "Re: Circular dependency - I think.."
- Reply: John Carson: "Re: Circular dependency - I think.."
- Reply: Karl Heinz Buchegger: "Re: Circular dependency - I think.."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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..
- Next message: tom_usenet: "Re: is using tons of stl a correct method?"
- Previous message: gswork: "Re: Hello World..."
- In reply to: John Carson: "Re: Circular dependency - I think.."
- Next in thread: John Carson: "Re: Circular dependency - I think.."
- Reply: John Carson: "Re: Circular dependency - I think.."
- Reply: Karl Heinz Buchegger: "Re: Circular dependency - I think.."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|