C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with reference initialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Fixa
Guest





PostPosted: Mon Dec 29, 2003 12:04 pm    Post subject: Problem with reference initialization Reply with 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??
Back to top
John Carson
Guest





PostPosted: Mon Dec 29, 2003 12:19 pm    Post subject: Re: Problem with reference initialization Reply with quote



"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





PostPosted: Mon Dec 29, 2003 12:21 pm    Post subject: Re: Problem with reference initialization Reply with quote



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





PostPosted: Mon Dec 29, 2003 1:20 pm    Post subject: Re: Problem with reference initialization Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.