You are not logged in.
I'm starting to learn C++ and I have a question. It's probably ridiculously simple
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)
Offline
yes, just like you prototyped should work.
Offline
yes, just like you prototyped should work.
Well, it didn't. Dunno why 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)
Offline
Or you could solve it with references:
class Foo {
public:
Foo () {};
void bar(Foo &f);
};
Offline
#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