Re: objects as parameters
From: Anthony C (AntMJ2317_at_comcast.net)
Date: 05/14/04
- Next message: Anthony C: "Re: objects as parameters"
- Previous message: Matthew Del Buono: "Re: strdup and memory leak ...."
- Maybe in reply to: Anthony C: "objects as parameters"
- Next in thread: Wolfie: "Re: objects as parameters"
- Reply: Wolfie: "Re: objects as parameters"
- Reply: Francis Glassborow: "Re: objects as parameters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 13 May 2004 22:33:06 -0400
osmium wrote:
> Anthony C writes:
>
>
>>I wrote a simple game in c++ and it uses a class object(cat) that ive
>>custom made. My question is this, i do not want to put a lot of code
>>into main as the code becomes jumbled and very unreadable with 4 pages
>>of code. I need to know how to pass my cat object(for example Garfield
>>in this case) as a parameter into another function. I've tried a few
>>things, but obviously have had no luck, any help is welcomed :-)
>
>
> I think the distinction between a class and an object is not clear to you.
> If I understand you, you want something like this:
>
> class cat {... stuff...};
>
> void foo(cat x) { ... stuff ...}
>
> int main()
> cat Garfield; // Garfield is a variable of (user defined) type cat
> foo(Garfield);
>
> Remember that the call on foo will necessitate making a copy of Garfield.
> Will the default copy constructor be adequate? Note that changes will be
> made to a *copy* of Garfield. You may want:to pass the argument by
> reference.
>
>
>
>
i have tried making a reference copy of the cat,im not getting anywhere,
this is what ive tried...this is just a snippet
void test(cat& catOne)
{
cout << catOne.getAge() << endl;
}
int main()
{
cat Frisky;
Frisky.setAge(5);
test(Frisky);
return 0;
}
the reference parameter is there, ive passed it in, unless im doing
something very wrong, that 'should' work, should it not?
unless your talking pointers and using the -> operator?
- Next message: Anthony C: "Re: objects as parameters"
- Previous message: Matthew Del Buono: "Re: strdup and memory leak ...."
- Maybe in reply to: Anthony C: "objects as parameters"
- Next in thread: Wolfie: "Re: objects as parameters"
- Reply: Wolfie: "Re: objects as parameters"
- Reply: Francis Glassborow: "Re: objects as parameters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|