Problem with reference initialization

F

Fixa

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??
 
J

John Carson

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);
}

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
}
 
R

Rolf Magnus

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);
}

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.
 
J

Jeff Schwab

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top