 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Fixa Guest
|
Posted: Mon Dec 29, 2003 12:04 pm Post subject: Problem with reference initialization |
|
|
I have a problem with reference initialization.
Maybe i shom my problem on simple example:
class foo
{
private:
...
public:
virtual void fun(void);
}
class getfoo
{
public:
static foo &get(int x)
}
class master
{
private:
foo &tmpfoo;
public:
master(char *filename) - from this file i get x which is used to
getfoo::get(x) function;
}
My question is: how i can initialize &tmpfoo??
|
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Mon Dec 29, 2003 12:19 pm Post subject: Re: Problem with reference initialization |
|
|
"Fixa" <fixa (AT) interia (DOT) pl> wrote
| Quote: | I have a problem with reference initialization.
Maybe i shom my problem on simple example:
class foo
{
private:
...
public:
virtual void fun(void);
}
class getfoo
{
public:
static foo &get(int x)
}
class master
{
private:
foo &tmpfoo;
public:
master(char *filename) - from this file i get x which is used to
getfoo::get(x) function;
}
My question is: how i can initialize &tmpfoo??
|
You can only do it in the constructor and only in the initialisation list,
e.g.,
master(char *filename, foo & f) : tmpfoo(f)
{
// stuff
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Mon Dec 29, 2003 12:21 pm Post subject: Re: Problem with reference initialization |
|
|
Fixa wrote:
| Quote: | I have a problem with reference initialization.
Maybe i shom my problem on simple example:
class foo
{
private:
...
public:
virtual void fun(void);
}
class getfoo
{
public:
static foo &get(int x)
}
class master
{
private:
foo &tmpfoo;
public:
master(char *filename) - from this file i get x which is used to
getfoo::get(x) function;
}
My question is: how i can initialize &tmpfoo??
|
I don't fully understand what you want. Where is the foo instance which
your reference is supposed to refer to? Generally, you initialize
references just the same as any other member variables, in the
initializer list.
Btw: You should post real code (but as short as possible) instead of
pseudo code. It often does a better job at making your intent clear.
|
|
| Back to top |
|
 |
Jeff Schwab Guest
|
Posted: Mon Dec 29, 2003 1:20 pm Post subject: Re: Problem with reference initialization |
|
|
Fixa wrote:
| Quote: | 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.
| Quote: | 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".
| Quote: | 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?
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|