: 1. Constructors can not (AFAIK) fail except maybe by throwing an
: exception. I wouldn't want to clutter parts of the code with
: exceptions unless all error checking is based on exceptions so the
: 'new' operator would have to be overloaded to do exception handling
: or some other ad hoc constructor failure checking. An alternative
: would be to stick to the current create_foobar and init_foobar
: functions but then we start losing some of the advantages of C++.
: This is related to the second problem:
Exception handling is a big issue in C++. You're thinking about something
that everyone has to think about :) This is where Java excels (and i do
not like java...).
You're right that constructors can't return 1 on failure but that's not
actually a problem. Its a design feature of OO languages (not just C++)
that makes it this way. There are two ways around this:
1) use exceptions. You just have be careful. Make sure that what you're
dealing with will not have leaks, etc... Here's a stupid example:
Myclass::Myclass(const char* something)
{
char* s = strdup(something);
if (s[0] == 'a')
throw new "Boom!";
// do something else
}
That's an obvious one that hits you. There are many more subtle ones
(like calling a function from a constructor that itself throws an
exception that you don't catch in the constructor but is thrown through
it, invalidating the object, etc...)
Most of the time, the way to get around exception memory leaks and such
is to use automatic classes or structs. The idea being that if they are
automatic, when they go out of scope, the destructors will be called
which will be coded to free memory and do whatever other cleanup needs
to be done. It saves you from remembering to delete in such a
situation. Thus:
Myclass::Myclass(const char* something)
{
string s(something);
if (s[0] == 'a')
throw new "Boom!";
// do something else
}
string is the class that will destroy the allocated memory what it goes
out of scope.
2) Build constructors that are exception free and use an init() method or
something similar that can return non-zero. A factory method can win
you some points here:
class X
{
private:
bool init;
public:
X() { init = false; }
int init(int thing)
{
if (thing == 5)
return 1;
else
{
init = true;
return 0;
}
}
};
class Myfactory
{
int construct_X(int thing, X* xp)
{
xp = new X();
if (xp->init(thing))
{
delete xp;
return 1;
}
else
return 0;
}
}
Also, not a great example... much more intelligent things can be done,
especially if you incorporate templates.
However, i wouldn't really shy away from constructors throwing
exceptions. They can make life very nice if they are used properly.
: 2. Is it possible to pass the necessary information to a function to
: create (with new) an arbitrary object with a known type of constructor
: or is a create_foobar callback function still needed? (See e.g.
: region_add_managed_new.)
Sorta depends on the particulars but something like this might be what
you're looking for:
template <class T>
T construct_it(int param1, int param2)
{
T x(param1, param2);
return x;
}
So you would call it like:
Myclass y = construct_it<Myclass>(5, 8);
I don't know Ion code and I don't know the particulars of what you want,
but that's probably the essence of it. With this, the work is done at
compile time to construct functions for each type you need, and you only
had to code one function.
Regs,
Derek
--
-----------------------------------------------------------
Derek Quinn Wyatt | Phone: (905) 886-7818 ext. 2313
Syndesis Engineering | Fax: (905) 886-9076
Toronto, Ontario, CAN | Email: [EMAIL PROTECTED]
-----------------------------------------------------------
"He was as lame as a duck. Not the metaphorical lame duck either,
but a real duck that was actually lame. Maybe from stepping on a
land mine or something."