| View previous topic :: View next topic |
| Author |
Message |
DIBUR Guest
|
Posted: Thu Dec 16, 2004 1:42 pm Post subject: Passing Dereferenced Pointer objects as arguments to functio |
|
|
Hi!!
if I run the following code will there be a problem
class X{
//defined
};
class Y{
X* ptr;
public:
int test(const X &);
};
int Y::test(*ptr) {
//do something
}
I ran a similar code and compiler gives an error something like this:
in function int Y::test(const X& ) an object expected in the
argument!!!
I cannot understand that when i have dereferenced the pointer then the
code should run normally as when we pass objects to such functions
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Dec 16, 2004 6:19 pm Post subject: Re: Passing Dereferenced Pointer objects as arguments to fun |
|
|
DIBUR wrote:
| Quote: | Hi!!
if I run the following code will there be a problem
class X{
//defined
};
class Y{
X* ptr;
public:
int test(const X &);
};
int Y::test(*ptr) {
//do something
}
I ran a similar code and compiler gives an error something like this:
in function int Y::test(const X& ) an object expected in the
argument!!!
I cannot understand that when i have dereferenced the pointer then the
code should run normally as when we pass objects to such functions
|
I don't see the call. I see a definition of an undeclared member
function.
You should have written:
int Y::test(const X &) { /* do something */ }
Somewhere else, you could write:
Y p;
X *x = new X();
p.test(*x);
and that would realize your wish of dereferencing a pointer to pass
a reference.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Fri Dec 17, 2004 4:39 am Post subject: Re: Passing Dereferenced Pointer objects as arguments to fun |
|
|
"DIBUR" <nishant404 (AT) yahoo (DOT) com> wrote
| Quote: | class X{
//defined
};
class Y{
X* ptr;
public:
int test(const X &);
};
int Y::test(*ptr) {
//do something
}
I ran a similar code and compiler gives an error something like this:
in function int Y::test(const X& ) an object expected in the
argument!!!
|
This code should not compile, because the declaration and definition of
Y::test have different signatures (not to mention that the definition has a
syntax error).
Once you post syntactically correct code, we can talk about what it means.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|