pointer to owner?

B

Brian

Hi all,

I have two objects, A, which has-a B. At the same time, B is related
enough to A, that it needs to call methods from A... I can only think
of how to do this with the following method. It seems like there must
be a better way, because I dont really like what I am doing... that
is, setting the B object's owner to A's this on construction. Does
anyone have any suggestions?


class A;

class B {
public:
int get_down_tonight() { return owner->do_a_little_dance(); }
void set_owner(A *a) { owner = a; }
private:
A *owner;
};

class A {
public:
int do_a_little_dance() {return j; };
A() {b.set_owner(this);
private:
B b;
int j;
};

main(void)
{
A myA;
int i;

i = A.b.get_down_tonight(); //call do_a_little_dance() from myA

}
 
V

Victor Bazarov

Brian said:
I have two objects, A, which has-a B. At the same time, B is related
enough to A, that it needs to call methods from A... I can only think
of how to do this with the following method. It seems like there must
be a better way, because I dont really like what I am doing... that
is, setting the B object's owner to A's this on construction. Does
anyone have any suggestions?


class A;

class B {
public:

Add:

B(A* o) : owner(o) {}
int get_down_tonight() { return owner->do_a_little_dance(); }
void set_owner(A *a) { owner = a; }
private:
A *owner;
};

class A {
public:
int do_a_little_dance() {return j; };

Superfluous semicolon.
A() {b.set_owner(this);

Mismatched curly brace. Replace the line above with

A() : b(this) {}
private:
B b;
int j;
};

main(void)

Non-standard C ALERT! Replace with

int main()
{
A myA;
int i;

i = A.b.get_down_tonight(); //call do_a_little_dance() from myA

Syntax error. Cannot use 'A' in this expression. Did you
mean to write

myA.do_a_little_dance();

?

Try not to repeat the mistake of typing your code directly into
the newsgroup posting. By now most operating systems support
some sort of copy-and-paste mechanism. Always post _real_ code
you have a question about.

Victor
 
J

Janusz Szpilewski

Brian said:
Hi all,

I have two objects, A, which has-a B. At the same time, B is related
enough to A, that it needs to call methods from A... I can only think
of how to do this with the following method. It seems like there must
be a better way, because I dont really like what I am doing... that
is, setting the B object's owner to A's this on construction. Does
anyone have any suggestions?

If A and B were POD ( C like Plain Old Data ) you could safely use the
standard macro 'offsetof' to calculate the address of the A object.
Otherwise such an operation is undefined even if I am pretty sure it
would work with most compilers.

Nevertheless to be compliant with the language standard get rid of the
constructors, visibility specifiers (public, private) and replace the
'class' keyword with 'struct'.

Then you can define a member function 'Owner' which returns a pointer to
the owner object:

#include <cstddef>

//...

A* B::Owner()
{
return reinterpret_cast<A*>(reinterpret_cast<char*>(this)-
offsetof(A,b));
}

Actually it does not look any nicer and such a trick should be used only
if you have to meet hard memory constraints.


Regards,
Janusz
 
B

Brian

Victor Bazarov said:
Add:

B(A* o) : owner(o) {}


Superfluous semicolon.


Mismatched curly brace. Replace the line above with

A() : b(this) {}

The problem with this, so it seems, is that during construction
(before the code block), this is not guaranteed to be defined. I get
compiler warnings when I do this reminding me of this fact.
Non-standard C ALERT! Replace with

int main()


Syntax error. Cannot use 'A' in this expression. Did you
mean to write

myA.do_a_little_dance();

?


Try not to repeat the mistake of typing your code directly into
the newsgroup posting. By now most operating systems support
some sort of copy-and-paste mechanism. Always post _real_ code
you have a question about.

Victor

Phew... you really ripped my code apart. The code I wrote was
intended to be an example of my question, not real code... The actual
example is really complex, and I did not believe that compilable code
was necessary for my question. I suppose that I could write up the
example, to make sure it compiles, but what is the point? I am asking
about concepts... I don't really care about actual syntax, as I
thought I made clear by naming my classes and methods the way I did.

Still, I understand that non-compileable code could be confusing...
using A instead of myA in main could have been (and was) confusing.

Thanks,
B
 
V

Victor Bazarov

Brian said:
"Victor Bazarov" <[email protected]> wrote in message

The problem with this, so it seems, is that during construction
(before the code block), this is not guaranteed to be defined. I get
compiler warnings when I do this reminding me of this fact.

'this' is well-defined. *this may not be fully constructed.
You're not supposed to call any member functions for (*this)
during construction. But if you only store it for some future
use, it's perfectly fine.

BTW, that's why the "reminders" are _warnings_ and not _errors_.
[..]
Try not to repeat the mistake of typing your code directly into
the newsgroup posting. By now most operating systems support
some sort of copy-and-paste mechanism. Always post _real_ code
you have a question about.

Victor

Phew... you really ripped my code apart. The code I wrote was
intended to be an example of my question, not real code... The actual
example is really complex, and I did not believe that compilable code
was necessary for my question. I suppose that I could write up the
example, to make sure it compiles, but what is the point? I am asking
about concepts... I don't really care about actual syntax, as I
thought I made clear by naming my classes and methods the way I did.

Still, I understand that non-compileable code could be confusing...
using A instead of myA in main could have been (and was) confusing.

From where I sit, it was impossible to tell whether you were simply
an inattentive typist or a complete newbie without a clue. That's
why usually people here will try to correct every mistake in code
posted. Do not take it personally.

Victor
 
R

Ron Natalie

Victor Bazarov said:
You're not supposed to call any member functions for (*this)
during construction.

Well you can, you just have to recognize that the object may not
be fully constructed. Having the constructor call other member
functions is not unheard of (frequently common parts of constructors
are handled this way).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top