Fixa said:
I have a problem with reference initialization.
Maybe i shom my problem on simple example:
class foo
{
private:
...
public:
virtual void fun(void);
}
You need a semicolon.
class getfoo
{
public:
static foo &get(int x)
}
You need two more semicolons.
Is getfoo supposed to be a function object? If not, why does its name
begin with a verb? If so, shouldn't it have an overloaded function
operator rather than a "get" method? Either replace "get" with
"operator ( )" use a function instead of a class, or change the name to
"foo_getter".
class master
{
private:
foo &tmpfoo;
public:
master(char *filename) - from this file i get x which is used to
getfoo::get(x) function;
}
Two more semicolons have escaped. Or wasn't this supposed to be C++
code you posted? Perhaps you have an extension where "-" means
"semicolon here, and the next two lines are comments."
I do like that neat trick where you use char* instead of const char*,
just to make sure users of this class will get a meaningful compiler
warning when they pass the constructor a string literal.
If the foo is temporary, why are you making a reference to it? And more
disturbingly, why are you wasting a member variable for something ephemeral?
My question is: how i can initialize &tmpfoo??
Why is tmpfoo a reference member? Why not just get rid of that '&' and
let tmpfoo be constructed along with the rest of a master? Then, you
could do this:
class master
{
foo tmpfoo;
public:
master( char const* filename ):
tmpfoo( get_foo( ) )
{ }
};
If it's expensive to copy a foo, do the initialization in foo's
constructor, and remove get_foo altogether. If you want to keep the
source of the initialization separate from foo, you could use the
Builder design pattern from the constructor.
-Jeff