Problem with reference initialization

Discussion in 'C++' started by Fixa, Dec 29, 2003.

  1. Fixa

    Fixa Guest

    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??
    Fixa, Dec 29, 2003
    #1
    1. Advertising

  2. Fixa

    John Carson Guest

    "Fixa" <> wrote in message
    news:
    > 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)
    John Carson, Dec 29, 2003
    #2
    1. Advertising

  3. Fixa

    Rolf Magnus Guest

    Fixa wrote:

    > 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.
    Rolf Magnus, Dec 29, 2003
    #3
  4. Fixa

    Jeff Schwab Guest

    Fixa wrote:
    > 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
    Jeff Schwab, Dec 29, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    937
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    437
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    458
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    932
    Victor Bazarov
    Sep 28, 2006
  5. Jess
    Replies:
    23
    Views:
    926
Loading...

Share This Page