Class Questions

Pages: 1234
There are more uses for pointers than just for dynamic allocation.
as a beginner, there's nothing at all wrong with learning how to use pointers first
When writing in C, I generally used pointers when passing variables directly to different functions or when dynamically setting aside contiguous blocks of memory with malloc()/calloc(). I had to do this a lot. In C++ when using classes, because all of the members are visible to one another, I've been able to get away from all that excessive parameter passing. Because I've written code in C for so long, it seems unnatural not to have to pass values by parameter so often. It's refreshing. However, are there times when using classes that you do need to pass values by parameter?
However, are there times when using classes that you do need to pass values by parameter?

I assume you're talking about whether you have to pass data members of an object to methods of that same class?

The only circumstance I can think of when you'd have to do that, is when you're calling a static method of the class. A static method exists outside of any single instance of the class; it therefore can't access the data members of any specific instance.

Obviously, code outside the class will need to pass in parameters to interface methods of the class.
When you say pass by parameter, do you mean passing member variables, or passing anything? Because there are loads of times when a classes functions need input from outside the class. That is unless you are following the god-class anti-pattern.
Sorry for the lack of clarity. I meant passing member variables.
@shadowmouse
Following your post I briefly read about what a god class is. A while back I made a game of TicTacToe. In this game I had one big TicTacToe class with every bit of data I used inside it. The member functions were in their own file, which was roughly 350 lines long. Would that be an example of a god class, or is it too small a project to be considered one?
It could well be, although it's only really a problem in the development stages. If you've finished it and it works, there's no need to change it. Without looking through all your code, it's not possible to check definitely, but any class that contains the data for more than one thing could be considered a god class. For example, a car class which purely consists of primitive types could be considered a god class and you could split it up into engine and wheels and so on. Basically if all your code is in one class and there is more than one thing that it handled by your program (it's really not possible to be more specific), then it could well be, but that doesn't mean it's wrong or bad. I often use a single god class while developing my program (or as I call it a brain class, a reference to mother brain from metroid), in which I put all the utility functions. If it would be easy to write the same code, but using several smaller classes instead of one, then yes, it is a god class, but it is up to you to decide if it is the best design situation.
Topic archived. No new replies allowed.
Pages: 1234