473,378 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

pointer to owner?

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

}
Jul 19 '05 #1
5 2950
"Brian" <ge******@pilot.msu.edu> wrote...
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
Jul 19 '05 #2
Brian wrote:
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
Jul 19 '05 #3
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<2sUpb.78213$275.211384@attbi_s53>...
"Brian" <ge******@pilot.msu.edu> wrote...
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) {}


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.
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


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
Jul 19 '05 #4
"Brian" <ge******@pilot.msu.edu> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message

news:<2sUpb.78213$275.211384@attbi_s53>...
"Brian" <ge******@pilot.msu.edu> wrote...
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) {}


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

Jul 19 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:9C9qb.82830$275.241756@attbi_s53...
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).
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Asfand Yar Qazi | last post by:
Hello. Partly for learning purposes, I have written a smart pointer class. Could you please tell me what's wrong with it? (I know there's something wrong with it, but just not what!) Note...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: Mike Mascari | last post by:
While migrating to 7.4, which performs quite nicely btw, I must have performed some sequence of the migration incorrectly. Now, when I use pg_dump on a database for backup, I get: pg_dump:...
11
by: Fred | last post by:
Hi I'm new to the std library. I have a function which reads data and creates a std string from it. I want to pass that back to the calling function as a parameter, not as the function return. ...
2
by: MLH | last post by:
Say I'm walking a subset of the records in tblAddnlOwners via DAO. Suppose there are 5 records in the extract and that I MoveFirst, MoveNext and MoveNext. Then, when on the 3rd of 5 records, I...
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
7
by: Tigera | last post by:
Greetings, I've been reading Scott Meyer's book, "Effective C++", and I think that I've confused myself terribly. I have a class like this: class Tile { private: Enchantment* m_ench;...
4
by: Kenneth Porter | last post by:
I'm trying to create a class that represents a bucket of locks, and I want to lock a set of objects specified by a container of pointers. I can't seem to find the right combination of function...
5
by: Soumen | last post by:
Hi, I've requirement to observe a raw pointer (i.e. I should be able to query if the pointer I'm using is still valid or not but when the observer goes out of scope, the resource -- memory --...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.