473,473 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hierarchy of objects, object "deep down below" needs to access one high up

Hey everyone!

I am (still) working on a project that I took over from former
students, so don't blame me for the criminal approach on coding *g*

The problem I have is fairly easy and while I have solutions, none of
them seems really "clean" to me and I was wondering if someone here
maybe had a better idea:

class geometry { ... }
class geometry2d : public geometry { ... }
class geometry3d : public geometry { ... }

class obj1 {
public:
int doSomething();
}

int obj1::doSomething()
{
// need to access Geometry2d from the main() function here somehow
}

class application {
private:
obj1* Obj1;
}

class environment {
public:
int run() { ... }
private:
application* Application;
}

int main (void) {
geometry2d* Geometry2d = new geometry2d();
environment* Environment = new environment();
Environment->run();
}

I hope I didn't make any grave syntax mistakes in the "sketch" of the
problem.
What it boils down to, is a bunch of objects that have a different
object pointer as a private property, this object is usually created in
their constructor. And then, some objects which are deep down that
chain, need to access the geometry object that was created in the main
function.
The current solution in the code was to implement a geometry argument
in each and every one of the object constructors and "pass it down" (as
I call it) from the main function. Now I find it pretty annoying to
have a bunch of constructor calls with that silly object argument just
because 5 levels lower, some mini-object needs to access it. It works,
but I don't like it.

Since the objects don't have the option of accessing a "parent" class
(project isn't designed that way), I was considering making the only
instance of the environment object a global variable, and storing the
needed geometry object pointer as a public property of the environment
class. I still like this solution better than the current one, but it
occured to me that object member functions accessing a global variable
isn't exactly clean programming style.

Does anyone have an idea that will eliminate the passing down of an
argument that isn't needed in most of the objects that pass it further
down, but will also avoid accessing a global object pointer from within
member functions?

Or can I get some people to assure me that using a global pointer to
the highest level object is "okay" programming here? *g*

Best Regards,

Lars

Dec 8 '05 #1
4 1993
lars.uffm...@rwth-aachen.de wrote:
Hey everyone!

I am (still) working on a project that I took over from former
students, so don't blame me for the criminal approach on coding *g*

The problem I have is fairly easy and while I have solutions, none of
them seems really "clean" to me and I was wondering if someone here
maybe had a better idea:

class geometry { ... }
class geometry2d : public geometry { ... }
class geometry3d : public geometry { ... }

class obj1 {
public:
int doSomething();
}

int obj1::doSomething()
{
// need to access Geometry2d from the main() function here somehow
}

class application {
private:
obj1* Obj1;
}

class environment {
public:
int run() { ... }
private:
application* Application;
}

int main (void) {
geometry2d* Geometry2d = new geometry2d();
environment* Environment = new environment();
Environment->run();
}

I hope I didn't make any grave syntax mistakes in the "sketch" of the
problem.
What it boils down to, is a bunch of objects that have a different
object pointer as a private property, this object is usually created in
their constructor. And then, some objects which are deep down that
chain, need to access the geometry object that was created in the main
function.
The current solution in the code was to implement a geometry argument
in each and every one of the object constructors and "pass it down" (as
I call it) from the main function. Now I find it pretty annoying to
have a bunch of constructor calls with that silly object argument just
because 5 levels lower, some mini-object needs to access it. It works,
but I don't like it.

Since the objects don't have the option of accessing a "parent" class
(project isn't designed that way), I was considering making the only
instance of the environment object a global variable, and storing the
needed geometry object pointer as a public property of the environment
class. I still like this solution better than the current one, but it
occured to me that object member functions accessing a global variable
isn't exactly clean programming style.

Does anyone have an idea that will eliminate the passing down of an
argument that isn't needed in most of the objects that pass it further
down, but will also avoid accessing a global object pointer from within
member functions?

Or can I get some people to assure me that using a global pointer to
the highest level object is "okay" programming here? *g*

Best Regards,

Lars


Global variables are undesirable. Also, data members should generally
be private or protected (cf.
http://www.parashift.com/c++-faq-lit....html#faq-19.8)
to promote encapsulation, and since your one-line constructors avoid
the unnecessary coupling between objects, the constructors seem like
the better way to go, slightly annoying though it might be. One other
thing to consider is that if your geometry2d (or environment or
whatever) object is unique throughout the system, you should consider
using the Singleton pattern (see _Design Patterns_ by Gamma et al. and
chapter 6 of _Modern C++ Design_ by Alexandrescu).

Cheers! --M

Dec 8 '05 #2
Sorry for the late reply, I got carried away with my computer gaming
addiction, and didn't get anything done on this project. Thank you for
your opinion, I think I'll go with passing down the parameter as you
suggested, even though it is annoying :)

Reading through parashift info now though, and it seems I'll not get
around buying some literature on standards if I want to get my
programming skills up-to-date again *g*. The way you mentioned that
book, it seems to be some sort of bible for C++ :)

Regards,

Lars

Dec 11 '05 #3
Is this a scenegraph application? If so, one of the ways that
scenegraphs solve the problem of carrying "global" data to the nodes is
by using the visitor pattern. The visitor interacts with the nodes by
traversing a graph of node objects. You could look at
http://www.openscenegraph.org as a reference.

Dec 20 '05 #4
la**********@rwth-aachen.de wrote:
Sorry for the late reply, I got carried away with my computer gaming
addiction,
I'll refrain from moral pronouncements. ;-)
and didn't get anything done on this project. Thank you for
your opinion, I think I'll go with passing down the parameter as you
suggested, even though it is annoying :)

Reading through parashift info now though, and it seems I'll not get
around buying some literature on standards if I want to get my
programming skills up-to-date again *g*. The way you mentioned that
book, it seems to be some sort of bible for C++ :)


_Design Patterns_ is a sort of Holy Writ for OO software today. _Modern
C++ Design_ is partly an application of _Design Patterns_ in C++ and
partly a tutorial on how to work wonders in the language, particularly
using templates. I'd say the former book is essential for anyone
working in an OO language (C++, Java, C#, etc.). The latter book is
very helpful, especially if you want to understand and use the Boost
and/or Loki libraries well, and I refer to it fairly often.

Cheers! --M

Dec 20 '05 #5

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

Similar topics

2
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
1
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
1
by: Jesper Denmark | last post by:
Hi, Is deep serialization possible using XML. Know any good tutorials?. By deep serialization I mean the ability that an object being serialized automatically serialize its members. E.g ...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
15
by: Matt Kruse | last post by:
Consider the following two functions: function func1() { var o ; if ( (o=document.forms) && (o=o) && (o=o.elements) && (o=o.sel) && (o=o.options) && (o=o)
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
6
by: =?ISO-8859-1?Q?Ignacio_Burgue=F1o?= | last post by:
Hi everyone. I'm dealing with some javascript code which uses eval to access properties of an object. For instance, I have the following: var events = {}; events.flatUsers = {};...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.