Pointers question (passing pointer to another function)

Discussion in 'C++' started by A, May 7, 2010.

  1. A

    A Guest

    Hi,

    I'd like to do something very simple but I'm not sure if I'm doing it right:

    I have 2 functions:

    //------------
    void Function1(TObject *obj)
    {
    Function2(obj);
    }

    void Function2(TObject *obj)
    {
    obj->CallSomeObjFunction();
    }
    //------------

    So basically Function1 simply calls Function2 and passes pointer to use it
    in Function2.

    The above code compiles and works, but I'm not sure about the scopes of obj
    pointer and if it can be used like this?

    Is the above usage of pointer correct or should I use &obj instead?
    A, May 7, 2010
    #1
    1. Advertising

  2. On 07.05.2010 02:46, * A:
    > Hi,
    >
    > I'd like to do something very simple but I'm not sure if I'm doing it right:
    >
    > I have 2 functions:
    >
    > //------------
    > void Function1(TObject *obj)
    > {
    > Function2(obj);
    > }
    >
    > void Function2(TObject *obj)
    > {
    > obj->CallSomeObjFunction();
    > }
    > //------------
    >
    > So basically Function1 simply calls Function2 and passes pointer to use it
    > in Function2.
    >
    > The above code compiles and works, but I'm not sure about the scopes of obj
    > pointer and if it can be used like this?
    >
    > Is the above usage of pointer correct or should I use&obj instead?


    Technically it's correct.

    Design-wise, though, in both functions the code assumes that the passed in
    pointer will be valid and non-null.

    That's best expressed in the language itself, by using references instead of
    pointers:


    void function2( Object& o )
    {
    o.someObjFunction();
    }

    void function1( Object& o )
    {
    function2( o );
    }

    int main()
    {
    Object blah;
    function1( blah );
    }


    Regarding scopes you're OK.


    Cheers & hth.,

    - Alf (blatant plug: my new (first) blog at <url: http://alfps.wordpress.com/>)
    Alf P. Steinbach, May 7, 2010
    #2
    1. Advertising

  3. A

    A Guest

    > Design-wise, though, in both functions the code assumes that the passed in
    > pointer will be valid and non-null.


    I understand what you mean, but that part should be ok. because in first
    function object is defined and non-null (and its pointer is passed as
    input).
    I simply use second function to have a sort of template (a lot of code that
    would otherwise be repeated identically in several functions):
    like this example:

    functionTemplate(object *obj, int iParamThatDiffers)
    {
    // do a lot of stuff here that is identical for functionA, functionB,
    functionC

    // Do obj specific stuff with param
    obj->DoStuffThatRequiresObj(iParamThatDiffers);
    }

    and then

    functionA(object *obj)
    {
    functionTemplate(obj, 1);
    }

    functionB(object *obj)
    {
    functionTemplate(obj, 2);
    }

    functionC(object *obj)
    {
    functionTemplate(obj, 3);
    }

    So I was mainly concerned about the scope of obj if I move it to other
    function like that.

    Thanks.
    A, May 7, 2010
    #3
    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. Newsgroup - Ann
    Replies:
    5
    Views:
    587
    John Carson
    Jul 30, 2003
  2. James Vanns
    Replies:
    7
    Views:
    7,008
    Evan Carew
    Jan 21, 2004
  3. jimjim
    Replies:
    16
    Views:
    821
    Jordan Abel
    Mar 28, 2006
  4. Vijai Kalyan
    Replies:
    4
    Views:
    686
    Vijai Kalyan
    Nov 8, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    650
Loading...

Share This Page