Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Pointers between two objects (C++)

Name: Anonymous 2006-11-11 13:17

Hi!

I'm trying to make two objects of two different classes who both point to each other; something like this:

#pragma once
#include "b.h"
Class a
{
 a(b* _ptr){ b = _ptr; }
 b* ptr;
};
____________
#pragma once
#include "a.h"
Class b
{
 b(a* _ptr){ a = _ptr; }
 a* ptr;
};

(Please forgive any syntax errors..)
Anyway; i can't include both .h files into each, it of course fucks the compiler up. So how would one go about doing this? I know i could just point directly into memory using some kind of empty template, but im not sure how to go about doing this

Name: Anonymous 2006-11-11 13:18

Gah; ofc its supposed to be ptr = _ptr instead of a/b = _ptr;.. i missed that, sorry :) It's not the solution though..

Name: Anonymous 2006-11-11 13:40

assuming that you're not stupid enough to be using #pragma once on a compiler that doesn't support it, just forward declare your classes instead of including them.  you only really need to include the full header if you're accessing members/methods of the class, or trying to include a complete object (rather than just a pointer).

#pragma once
// #include "b.h"
class b;
class a
{
 a(b* _ptr){ ptr = _ptr; }
 b* ptr;
};


Name: Anonymous 2006-11-11 14:15

Ofc im not using some old piece of garbage.

Thank you for the input, this should work great, even though it feels a bit like a hack.

Name: Anonymous 2006-11-13 4:31

          ∧_∧   / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
          ( ´∀`) < 

        /    |    \
       /       .|      ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
       / "⌒ヽ |.イ |
   __ |   .ノ | || |__
  .    ノく__つ∪∪   \
   _((_________\
    ̄ ̄ヽつ ̄ ̄ ̄ ̄ ̄ ̄ | | ̄

Name: Anonymous 2006-11-13 5:36

even though it feels a bit like a hack.
If pointing to a structure or a class were a hack, so many data structures and things would be broken. Like, without pointers to classes the pimpl idiom is just stupid.

Name: Anonymous 2006-11-13 12:48

>>4
Forward declarations are not a hack. C and C++ are parsed in a single pass, meaning that you need to hint the compiler that "a" is really a class type rather than a typo.

(I think in GNU G++ you could say "class b *_ptr;" and the forward declaration would be made right there, but that has some issues with namespace scoping and such and so it's deprecated.)

Name: Anonymous 2006-11-15 6:03

bump

Name: ​​​​​​​​​​ 2010-09-07 21:44

Name: Anonymous 2010-12-17 1:19

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Anonymous 2010-12-17 1:30

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Anonymous 2011-02-04 17:59

Name: Sgt.Kabuጉ퐚kimanﰰꨶ 2012-05-28 20:50

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List