You are not logged in.

#1 2010-07-11 11:07:51

dcc24
Member
Registered: 2009-10-31
Posts: 732

C++ question: foo object as a parameter for a method of class foo

I'm starting to learn C++ and I have a question. It's probably ridiculously simple smile

Let's say I have a class "Foo". There is also a method "bar" of class "Foo". For example:

class Foo {
    public:
        Foo () {};
        void bar();
};

I'd like to pass another object "Foo" as a parameter to "bar". i.e:

class Foo {
    public:
        Foo () {};
        void bar(Foo f);
};

Is this possible? If yes, how?


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#2 2010-07-11 11:12:48

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,487
Website

Re: C++ question: foo object as a parameter for a method of class foo

yes, just like you prototyped should work.

Offline

#3 2010-07-11 11:14:55

dcc24
Member
Registered: 2009-10-31
Posts: 732

Re: C++ question: foo object as a parameter for a method of class foo

Allan wrote:

yes, just like you prototyped should work.

Well, it didn't. Dunno why hmm Passing a pointer of that object, however, compiles fine. i.e:

class Foo {
    public:
        Foo () {};
        void bar(Foo * f);
};

Last edited by dcc24 (2010-07-11 11:27:43)


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#4 2010-07-11 12:07:35

PJ
Member
From: Sweden
Registered: 2005-10-11
Posts: 602

Re: C++ question: foo object as a parameter for a method of class foo

Or you could solve it with references:

class Foo {
    public:
        Foo () {};
        void bar(Foo &f);
};

Offline

#5 2010-07-11 12:08:06

Zeist
Arch Linux f@h Team Member
Registered: 2008-07-04
Posts: 532

Re: C++ question: foo object as a parameter for a method of class foo

#test.cpp

class Foo {
      public:
            Foo () {};
            void bar(Foo f) {};
};

int main()
{
      Foo test;
      Foo test2;
      test.bar(test2);
}

Just had to test making a complete cpp-file. That compiles and runs absolutely fine in both GCC 4.5.0 and 4.0.1. (Which is what I had quickest access to to test this). So I am not sure why it wouldn't work for you.


I haven't lost my mind; I have a tape back-up somewhere.
Twitter

Offline

Board footer

Powered by FluxBB