Re: objects as parameters

From: Anthony C (AntMJ2317_at_comcast.net)
Date: 05/14/04


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?



Relevant Pages

  • Re: [PATCH] Make futex waiters take an mm or inode reference
    ... Make futex waiters take an mm or inode reference ... Rusty Russell wrote: ... +static inline void drop_key_refs ... -static int futex_wake ...
    (Linux-Kernel)
  • Re: dynamic type checking - a pauline conversion?
    ... it depends on the type of reference to the object. ... In Smalltalk the reference doesn't have a type but the instance does ... and C++ can not guarantee type correctness at compile ...
    (comp.object)
  • Re: Simplicity
    ... > ByRef (by reference) ... > Dim k as Zone ... property int Value; ... Microsoft C/C++ Optimizing Compiler Version 14.00.40809 ...
    (comp.lang.cpp)
  • Re: hash two keys to one index
    ... What goes into the map are pairs of (reference to key, ... When I insert an object into the hash table, I pass in ... void insert(Object obj, int hash) throws HashTableFull ... int probe = 0; ...
    (comp.lang.java.programmer)
  • Re: Assigning to references
    ... a reference ... behaves exactly like the object it refers to (it is an "alias" for that ... int a = 1; ... Bear in mind that, unlike a reference, a pointer will not extend the ...
    (comp.lang.cpp)