'x = this' in constructor

Discussion in 'C++' started by Alex Vinokur, Dec 4, 2005.

  1. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Dec 4, 2005
    #1
    1. Advertising

  2. Alex Vinokur

    Guest

    Alex Vinokur wrote:
    > Is this safe?
    >
    > -----------------
    > struct Foo;
    >
    > struct Bar
    > {
    > Foo* f;
    > };
    >
    > struct Foo
    > {
    > Bar b;
    > Foo()
    > {
    > // Stuff
    > b.f = this; // Is that safe?
    > // Stuff
    > }
    > };
    >


    So long as Bar.f is a pointer I think you are ok. It's a cyclic
    dependency though and best avoided if possible...sometimes it isn't.

    http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple
     
    , Dec 4, 2005
    #2
    1. Advertising

  3. Alex Vinokur

    TuxC0d3 Guest

    It depends on how you use it.. But i don't see anything wrong with
    this. Just be careful that you don't destroy things more than once,
    which might lead to undefined behaviour..

    For instance, this would not be wise:
    Foo* FooObj = new Foo;
    //... use FooObj
    delete FooObj->b.f; // Becouse f points to the same object that FooObj
    points to, that object gets destroyed here..
    //... The object that FooObj points to is no longer in existance here,
    so..
    FooObj->b.f = FooObj;
    // That might look like restoring the address of the object that FooObj
    *WAS* pointing to in b.f..
    // But in fact, it is an error situation, becouse the object that
    FooObj was pointing to, doesn't
    // exist anymore and on most implementations (if not all), this would
    couse undefined behaviour or couse an error.
    delete FooObj; // This also is an error, becouse you're trying to
    destroy an object that was already down the drain..
     
    TuxC0d3, Dec 4, 2005
    #3
  4. Alex Vinokur

    Kaz Kylheku Guest

    wrote:
    dependency though and best avoided if possible...sometimes it isn't.
    >
    > http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple


    So what? That page is just someone's ill-informed, inexperienced
    opinion.

    Just because someone dropped something into a Wiki doesn't mean it has
    become gospel.
     
    Kaz Kylheku, Dec 5, 2005
    #4
  5. Alex Vinokur

    Pete Becker Guest

    Kaz Kylheku wrote:
    > wrote:
    > dependency though and best avoided if possible...sometimes it isn't.
    >
    >>http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple

    >
    >
    > So what? That page is just someone's ill-informed, inexperienced
    > opinion.
    >
    > Just because someone dropped something into a Wiki doesn't mean it has
    > become gospel.
    >


    But this one is a "Principle"! It SAYS so! And it's based on what
    someone else wrote, and even includes a link! So it must be true!

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Dec 6, 2005
    #5
  6. Alex Vinokur

    Guest

    Pete Becker wrote:
    > Kaz Kylheku wrote:
    > > wrote:
    > > dependency though and best avoided if possible...sometimes it isn't.
    > >
    > >>http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple

    > >
    > >
    > > So what? That page is just someone's ill-informed, inexperienced
    > > opinion.
    > >
    > > Just because someone dropped something into a Wiki doesn't mean it has
    > > become gospel.
    > >

    >
    > But this one is a "Principle"! It SAYS so! And it's based on what
    > someone else wrote, and even includes a link! So it must be true!


    Joke around all you want guys but these things are well worth knowing
    or at least familiarizing yourself with. As anyone who has actually
    read these articles and principles knows, it is stated in several
    places that some of these principles are actually contradictory.
    Everything needs balance.

    Cyclic dependencies can become a major hassle. The principle I cited
    above applies to packages mainly but the reasoning in it can be applied
    to classes also to a lesser degree.
     
    , Dec 6, 2005
    #6
  7. Alex Vinokur

    Pete Becker Guest

    wrote:
    >
    > Cyclic dependencies can become a major hassle. The principle I cited
    > above applies to packages mainly but the reasoning in it can be applied
    > to classes also to a lesser degree.
    >


    Since packages aren't part of C++, it's rather difficult to assess that
    statement. And since the link goes to a page with a rather rambling
    discussion that's mostly lacking in actual analysis, it's not
    particularly helpful.

    There's nothing wrong with cyclic dependencies among classes when that's
    what the problem domain requires.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Dec 6, 2005
    #7
  8. Alex Vinokur

    Guest

    Pete Becker wrote:
    > wrote:
    > >
    > > Cyclic dependencies can become a major hassle. The principle I cited
    > > above applies to packages mainly but the reasoning in it can be applied
    > > to classes also to a lesser degree.
    > >

    >
    > Since packages aren't part of C++, it's rather difficult to assess that
    > statement. And since the link goes to a page with a rather rambling
    > discussion that's mostly lacking in actual analysis, it's not
    > particularly helpful.


    Obviously you guys just want to get into a flame war.

    Packages are not defined by the standard but in practical terms they
    are definately part of the C++ programmer experience.

    Most people understand that cyclic dependencies are best avoided (yes,
    in classes too) if practical so I'll just leave it at that now.
     
    , Dec 6, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,046
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,337
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,823
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    467
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,241
Loading...

Share This Page