473,372 Members | 1,380 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,372 software developers and data experts.

Passing objects around

According to what I have learnt so far, instantiating global objects should
be the last resort. Is there any reasons why so?

Sometimes some objects or their pointers have to be shared among several
other objects, I'd like to know how this is handled commonly. Could someone
elaborate on this?

Another situation is some objects could be shared among several threads.
How to handle it commonly?

Thanks for your comments!
Jul 22 '05 #1
6 2273
"Garma" <Ga*****@nosysserv.com> wrote...
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so?
Usually because they are accessible to anyone at any time. Dangerous.
Sometimes some objects or their pointers have to be shared among several
other objects, I'd like to know how this is handled commonly. Could someone elaborate on this?
It depends on what you mean by "shared".

The usual way is 'association' which is done by storing a pointer to
a shared object in an instance of another class. But it can be different
according to requirements.
Another situation is some objects could be shared among several threads.
How to handle it commonly?


If your threads are represented by objects, see above. If not, you should
probably ask in comp.programming.threads since threading is not something
C++ defines and as such tends to be OT here.

Victor
Jul 22 '05 #2
"Garma" <Ga*****@nosysserv.com> wrote in message
news:9C********************@bgtnsc05-news.ops.worldnet.att.net...
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so?

Sometimes some objects or their pointers have to be shared among several
other objects, I'd like to know how this is handled commonly. Could someone elaborate on this?

Another situation is some objects could be shared among several threads.
How to handle it commonly?

Thanks for your comments!


Consider:

int yadda; // global variable

void foo() // set the global
{
yadda = 1;
}

void bar(); // defined elsewhere

int main()
{
foo(); // set yadda to 1
bar(); // does bar change yadda? maybe!
}

All the function bar has to do is declare

extern int yadda;

and it has full read/write access to your global variable. Ouch. Let us
refactor:

void foo(int &yadda) // set the given variable (no globals)
{
yadda = 1;
}

void bar(); // defined elsewhere

int main()
{
int yadda; // local variable rather than global
foo(yadda); // set yadda to 1
bar(); // yadda is still 1 -- bar cannot access it
}

This particular maintainability nightmare can be avoided other ways such as
declaring yadda global in an unnamed namespace, but you can have similar
problems with globals all within the same translation unit -- e.g. another
programmer on your team takes a liking to your global variable and starts
writing values to it, thus introducing bugs into your (not his) code. :P The
advice you have heard about avoiding global variables is quite sound.

I do use them in some cases, however. For instance when writing DLL's I
sometimes find no reasonable alternative for certain operations. I console
myself that my DLL is really just a great big class and the globals within
are analogous to private variables in a real class -- not so terrible when
you think of it that way.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
Garma wrote:
According to what I have learned so far,
instantiating global objects should be the last resort.
No.
Global *constants* are just fine and their use is *encouraged*.
Is there any reasons why so?
Global variables are evil.
Any function can modify a global variable
and you may be obliged to inspect *every* function
to find the one that sets it to an incorrect value.
Sometimes some objects or their pointers
must to be shared among several other objects.
I'd like to know how this is handled commonly.
They must be passed through a function argument list
by value, reference or a pointer to the object.
Pass a const reference or a const pointer
unless the object must be modified "in place".
Otherwise, return an object by value so that
it can be used in an expression or a definition.

class X { /* . . . */ };

X f(void);

const X x = f();
Could someone elaborate on this?
Another situation is some objects could be shared among several threads.
How to handle it commonly?


Jul 22 '05 #4
On Thu, 15 Jan 2004 02:08:05 +0000, Garma wrote:
According to what I have learnt so far, instantiating global objects should
be the last resort. Is there any reasons why so?
Another reason, apart from what was already mentioned, is order of
initialisation. Globals in one source file are initialised in the order
they are declared, but in what order the source files are initialised is
undefined.

For small projects, you probably could'nt care less, but for larger
programs, this quickly becomes a nightmare.

Sometimes some objects or their pointers have to be shared among several
other objects, I'd like to know how this is handled commonly. Could
someone elaborate on this?
There are many ways, depending on your situation.

1) Redesign. Do you really need it? Is it not just bad design?

2) Ask yourself, what concept does this global represent? Create a class
that represents the concept, never use raw pointers or other values as
globals. Often you'll find you can cluster groups of former globals behind
one interface, using only one global of this new class (also see point 4).

3) Use a singleton. Search google for more info, there are litterally
hunderds of ways how toimplement this in C++.

One way would be:

template<typename T>
class Singleton {
static T& instance() {
static T inst;
return instance;
}
};

Use as follows:

//instantiate as a global:

Singleton<myclass> mysingleton;

// use the object:

void f() {
mysingleton::instance().doSomething();
}

4) Pass around as parameters. Often some (former) globals can be grouped
into one class and a pointer to an object of this class can be passed
around. Obviously these objects should be related and should have been
grouped in a class anyhow. This strongly relates to point 2.

5) If it has to be shared between similar objects, does a common baseclass
help? Maybe containing a static? Or containing a pointer to whatever they
all need access to? Again, think in concepts, not in implementations. A
good concept will (often, not always) spell out the (correct)
implementation.

6) Create one 'god' class that contains all the globals, and only make
that global, or make it a singleton. This is often a sign of bad design,
but...

7) ... call that god class Application and you have a very common
framework setup.

Something I sometimes do (using another form of singleton):

class MyApp {
public:
MyApp();
int run(int argc, char **argv);
static MyApp& instance() { return *inst_; }
// Other member functions here
private:
MyApp *inst_;
// other data members here
};

MyApp::MyApp()
{
assert(!inst_);
inst_ = this;
}

int main(int argc, char **argv)
{
try {
return MyApp().run(argc, argv);
} catch (const ....) {
// lots of error handling here for runaway exceptions
}
}

// somehwere else

void DeepDown::somewhereintheprogram()
{
// how we warn depends on how the program is invoked.
MyApp::instance().emitWarning(...);
...
}
Another situation is some objects could be shared among several threads.
How to handle it commonly?


Use any of the ways given above, but with threads it is even more
important to limit the use of globals. I most often use stack based
objects created somewhere in main or quickly after that, so they stay in
scope for the lifetime of the program. By passing the right arguments to
the threads the threads have pointers to these objects. Obviously these
objects should have ways to synchronize access to them. Also obviously,
you should make sure all subthreads have exited before these objects pass
out of scope.

Whatever you do, in the face of threads you want your accesses to be
tightly controlled. This means wrapping everything in classes that govern
the synchronization for access. Wether this is a global, a singleton or
whatever does not matter. Multi-threaded programming can be hard, but
mainly because programmers don't follow the above advice. If you do it the
way I say here, thread based programming suddenly ceases to be the big
problem it is often said to be! (But note that this is OT for this group!)

HTH,
M4
Jul 22 '05 #5

"Garma" <Ga*****@nosysserv.com> wrote in message
news:9C********************@bgtnsc05-news.ops.worldnet.att.net...
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so?
I wouldn't say that exactly. In small programs, it's often a first resort.
Sometimes some objects or their pointers have to be shared among several
other objects, I'd like to know how this is handled commonly. Could someone elaborate on this?
It could be global, if that's appropriate. Other solutions include putting
them in some other object, putting them in a namespace, adding public
functions to access them, or specifying friends to access them.
Another situation is some objects could be shared among several threads.
How to handle it commonly?


That is more likely going to be a problem related to your specific
environment, and you should ask on the specific newsgroup.
Jul 22 '05 #6
Martijn Lievaart wrote:
3) Use a singleton. Search google for more info, there are litterally
hunderds of ways how toimplement this in C++.

One way would be:

template<typename T>
class Singleton {
static T& instance() {
static T inst;
return instance;
}
};

Use as follows:

//instantiate as a global:

Singleton<myclass> mysingleton;


I have a problem with this singleton approach, in that anybdy can still
create other instances of myclass. If he really needs the existance of
exactly one element of a particular class, then it is better for him to
design his class a singleton, not use the template collaboration.

NOTE: If all he wants is a global instance, ignore the design comments,
the template will work...
Jul 22 '05 #7

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
3
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing...
4
by: Jack | last post by:
Hi, I have a hashtable that I need to pass around to different Business Objects. My question is it better to pass it and make a locale hashtable variable and set it equal to the passed hashtable...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
3
by: Stephen Travis | last post by:
I'm trying to write a subroutine that will fill an array of some type with several objects of that type. The subroutine works fine if I directly reference the array but if I pass the array as an...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.