Using member functions in another classes

I'm doing a small messaging program right now and I decided to try and make my code more solid by dividing it up into classes for each task etc. That way I don't have one huge ugly class doing all the work and I feel the code will get more readable and easier update.

I'm having a very confused moment right now though, as my structure of the code is getting cleaner the connections are getting way more complicated, a little bit too complicated actually. To give you an example I have a user class which stores information about the user etc. The user class also handles writing messages from the user but this message is then sent to another class when submitted which takes care of processing the message and save it to a file. So my problem is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
{
public:
const vector<char>& getMessage() const;

private:
std::vector<char> message;
};

class B
{
public:
void processMessage();

private:
A message;
};


This is what confuses me. I need to pass the vector by reference from class A to class B. I've been googling around and I've seen that in order to easily pass member functions I need to declare them as static. But in my case this is impossible because my vector is not static. So what do i do? Do I create an instance of class A in class B and access the function that way?, Am I not then creating a completely new instance of this class and doing unnecessary copying?
what is the best way to do this?
Having a class for each function; how is that better than just having the function?

The point of defining a class is to be able to create more than one of them, which will then exist as independent objects. If you make a class just to stick a fcuntion in it, you then either have to create an object of that class just to use the function, or you end up making the function static, in which case you've basically got a function, like you had before, with a whole load of extra crap around it.


That way I don't have one huge ugly class doing all the work

You don't have to have a class. It's not compulsory. If there's no sense in making one, don't make one. You can just have functions.
Last edited on
well yeah I guess I dont' really need to have a class, but I meant that I have a class for each functionality within the program.

actually not having a class would make the whole process alot easier. I don't need more than one instance of each function.

I'm still quite curious about how function methods can talk to each other within different classes, I've been wondering about that for quite a while.

thanks Moschops for the advice, I guess I've gone a little class crazy ever since I got to know them ;)
I'm still quite curious about how function methods can talk to each other within different classes, I've been wondering about that for quite a while.

First up, you need to double-check your terminology. Function and method are two different words used for the same thing; "function methods" is meaningless. I presume you meant "class methods" or "class functions".

It might be easier if you think of it as how do classes talk to each other. That, after all, is the whole point of OOP - to make it easier for you, the programmer, to think about how to solve the problem, by letting you think in terms of objects, and easier to code the solution, by letting you create objects that mirror the solution inside your head. It's all to make it easier for you to think and code. When using objects makes things harder, it's become pointless.

Here's a simple example of an object of one class calling functions in a different class; a display class that is used to show a user information about people, where each person object is an instance of the personClass type.

1
2
3
4
5
6
personClass somePerson;
displayClass someDisplay;

// Lots of code setting things up, reading data etc etc.

someDisplay.showInfoAboutAPerson(somePerson);

So how does this object, someDisplay, which is of type displayClass, actually talk to the object somePerson (which is of type personClass)? By calling one of somePerson's functions - perhaps like this:
1
2
3
4
5
6
7
8
9
displayClass::showInfoAboutAPerson ( personClass thePerson)
{
// How does the display get data about the person to display it?
//  Simple; the person object has functions that give back data about itself
int ageToDisplay = thePerson.ageOfPerson();
int heightToDisplay = thePerson.heightOfPerson();

// Code to actually do something with the data
}


Last edited on
Topic archived. No new replies allowed.