Passing this as an argument

Discussion in 'C++' started by persres@googlemail.com, Jul 3, 2012.

  1. Guest

    Can I pass this as any argument? Why cant we do something like the
    following?.

    // main.cpp
    int main()
    {
    extern ABCD *abcd;
    ABCD::func(abcd, 3);
    }

    //abcd.h
    struct ABCD
    {
    void func(int i);
    }
     
    , Jul 3, 2012
    #1
    1. Advertising

  2. On 7/3/12 7:15 AM, wrote:
    > Can I pass this as any argument? Why cant we do something like the
    > following?.
    >
    > // main.cpp
    > int main()
    > {
    > extern ABCD *abcd;
    > ABCD::func(abcd, 3);
    > }
    >
    > //abcd.h
    > struct ABCD
    > {
    > void func(int i);
    > }
    >
    >



    Because that isn't the syntax.

    To do what you appear to want to do you could use

    abcd->func(3);

    to call the member function.

    Note that while it is sometimes useful to think of the "this" parameter
    as just an extra hidden parameter, the standard does not require that it
    be passed in the same manner as a regular parameter, and I do know of
    compilers where "this" was passed to member functions differently than
    an ordinary parameter to a function. The designer of the compiler
    figured that the member function likely did a number of accesses to the
    member being passed, so this was passed in a register designed for
    indexed access of a block of memory, while ordinary parameters will
    passed via the stack.
     
    Richard Damon, Jul 3, 2012
    #2
    1. Advertising

  3. On 7/3/2012 7:15 AM, wrote:
    > Can I pass this as any argument?


    Usually. Unless something is not correct.

    > Why cant we do something like the
    > following?.


    Because that's not C++ syntax.

    >
    > // main.cpp
    > int main()
    > {
    > extern ABCD *abcd;


    What's "ABCD" here? Did you forget to include 'abcd.h'?

    > ABCD::func(abcd, 3);


    Presuming the compiler knows what 'ABCD' struct is, non-static functions
    are only callable using the "dot" or the "arrow" form with an object or
    a pointer to an object, respectively. So, with your current declaration
    of 'abcd' as a pointer you must write

    abcd->func(3);

    > }
    >
    > //abcd.h
    > struct ABCD
    > {
    > void func(int i);
    > }


    A semicolon is missing here.

    >
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 3, 2012
    #3
  4. MelissA Guest

    On Tue, 3 Jul 2012 04:15:59 -0700 (PDT)
    "" <> wrote:

    > Can I pass this as any argument? Why cant we do something like the
    > following?.
    >
    > // main.cpp
    > int main()
    > {
    > extern ABCD *abcd;
    > ABCD::func(abcd, 3);
    > }
    >
    > //abcd.h
    > struct ABCD
    > {
    > void func(int i);
    > }
    >
    >


    Yes we can do that ;)
    This is example (modified to compile) taken from:
    http://www2.research.att.com/~bs/C 0xFAQ.html#std-function

    #include <functional>
    #include <iostream>
    using namespace std;
    using namespace std::placeholders;

    struct X {
    int foo(int i ){ return 2*i; }
    };

    int main()
    {
    function<int (X*, int)> f;
    f = &X::foo; // pointer to member

    X x;
    int v = f(&x, 5); // call X::foo() for x with 5
    function<int (int)> ff = std::bind(f,&x,_1); // first argument for f is &x
    cout<< (v=ff(5))<<endl; // call x.foo(5)
    }
     
    MelissA, Jul 3, 2012
    #4
  5. Balog Pal Guest

    "" <>
    > Can I pass this as any argument? Why cant we do something like the
    > following?.
    >
    > // main.cpp
    > int main()
    > {
    > extern ABCD *abcd;
    > ABCD::func(abcd, 3);
    > }


    As other already pointed out, you CAN do the call using the synax
    abcd->func(3);

    The "uniform call", whare you could call member functions as free functions
    of free functions as member funcitons, and the compiler automagically would
    do the conversion was proposed frequently, and indeed would help much wish
    many common situations.

    It was routinely rejected for practical reasons, for one, it would mess with
    name lookup -- a thing that is complex enough as it is. For the other, it
    would change meaning of existing code, introducing cases where old code
    stops compiling due to ambuguity or the code compiles but calls a different
    function than it did before, subtly changig behavior.
     
    Balog Pal, Jul 8, 2012
    #5
    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. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    421
  2. Ben Kial
    Replies:
    1
    Views:
    703
    Eric Enright
    Nov 15, 2004
  3. Trans
    Replies:
    3
    Views:
    208
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    266
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    198
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page