Passing this as an argument

P

persres

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

Richard Damon

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

Victor Bazarov

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
 
M

MelissA

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

Balog Pal

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top