function with class as argument

Hi,

I have to manipulate a given object. Since i don't want to mess around with the code someone else wrote i want to write a class which inherits the objects class.
In this class i want to define my function which has the obects class as argument and returns the object.

So its something like:

1
2
3
4
class mother;
class child : mother;

mother child::function(mother)


That looks a bit strange to me and i guess my function can just manipulate members of the class it's defined in.
So do I need another class which has child as member and a function of this member?

That would look like:
1
2
3
4
5
anotherclass{
public: 
  child Boy;
  child anotherclass::function(child);
}



That all seems to be a bit inconvenient. I don't like it.
There has to be an easier way...

I really appreciate your help.

Alex
Hi, I don't think there is any thing wrong with your first way. Arguments mounted to a member function and the object replied by it can be anything, in particular the object belonging to the that class itself, or "mother class" too. Did you try that and get errors?
Last edited on
Yeah i get an error when i try to call the function. That's why i thought that a function can't have the class in which it is defined as argument.

My function call is:

1
2
3
4
5
6
7
8
9
class child{
public:
  mother child::function(mother);
};

//function call  in main program

mother Mutter;
Mutter = child::vary(Mutter);


I get the following error:

main.cc: In function `int main()':
main.cc:14: cannot call member function `mother child::vary(mother)' without
object

After i saw that error i thought i need an object on which the function may work such as object.function() .
That can be achieved through the second way i explained above. So u have the following function call:

variabel = anotherclass::child.function(child) ;

Thank you for help.
Don't be confused because of child and mother. Actually they are the same because of inheritance. I just wanted to make clear that i don't want to change the class mother and that's why i created child.

Alex



In the "main function" (not in another class) you just declare an object that has the method, namely child Boy and call Boy.function(mother object).
Other way to do it is to pull the function out of the class "child", it becomes a free function and will be available to main with out any object needed. This look nicer when the function is general and needs not to belongs to any object.
Last edited on

So
1
2
3
4
 child Kind;
 mother Mutter;

Mutter = Kind.vary(Mutter);


would be correct? When i try this i get an error:

undefined reference to `child::vary(mother)'

Yeah, i know that i can define the function in the main function, but i don't want to do this because that's not my code either.

I don't like the fact that one needs an object to call the function defined in the class of that object.
Is there a way to define a function in a class so it's still free and doesn't need an object of that class.
I just want a function with which i can manipulate my objects of the class.

Mhh, I think I'm gonna create a new class. (the second way)



I think that means there was something wrong in the definition of your class, so that the compiler doesn't see your function... not because of the way you call it. I guess you just defined the header of the function, did you have the body of the function available to the compiler? (sorry for this stupid question but I just guess you put it somewhere else :-D)

You need not to put the function into the main function, just in the global space as any other functions, or if you want you can put them in some namespace. You can put it in another file.cpp and using include "file.cpp", too.

1
2
3
4
5
6
7
class mother {}

class child{}

mother function(mother){}

int mai() {}


I understand the fact that one needs objects to call function is the main point of object oriented style, which recently I find very convenient to construct programs.
Hi,

Yesterday i had no more time to work on it but now i read your post and continued.


I guess you just defined the header of the function, did you have the body of the function available to the compiler? (sorry for this stupid question but I just guess you put it somewhere else :-D)


No stupid question. ^^

Everything can happen to a newbie like me. But i had the body and the declaration of the function, i just forgot the child:: before the definition of my function.

Thank you for your help.
Topic archived. No new replies allowed.