C++ where to write functions of a class? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In my C++ project I have 2 files: trip.h trip.cpp
If I want to write a helper function that takes two ints and returns the multiplication of them where should I write it and how (const, static etc...)? Note: I don't want the user to be able to use it if that is possible as in C language when we declare it as static.
if a function needs to access private members we declare it as a friend inside the class.
But, should I write its title again outside the class in the .h file and write its definition in the .cpp file? Note: I was asked to make it an external function which means written outside the class.
Edit: I want answers to the exact questions above, I know I can solve this by different ways as suggested but I want to stick to this method

I would declare and implement the function in trip.cpp as a static function. C++ is backwards-compatible with C so anything from C applies. As you already know, declaring the function static limits is visibility to that object file.
I would use a static private method for that class. Static here means something different than #1 - the new meaning is that it can be invoked without an association to any instances for the class it is declared under. Private meaning it can call that class's private methods (provided it has a handle to an object first). If the function is relatively short, I would just implement it in the .h file - though many prefer it in the .cpp file.

Related

C++ - Access All Functions Of Private Member Variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I get the feeling that this is a duplicate/stupid question, but, despite my best efforts, I don't know what to Google to find anything.
Anyhow, I would like to have a class BigClass that contains another class SmallerClass as a private member variable so that people using BigClasscan't access the public member variables that SmallerClass contains.
However, I also want people to be able to manipulate SmallerClass via it's public methods. I want to be able call bigClassInstance.SmallerClassMethod() without having to make a bunch of methods inside BigClass that just call a homonymous method inside SmallerClass.
Is this possible? If so, how?
Note: I did consider getting BigClass to inherit from SmallerClass, but, this doesn't work as other things that are accessing SmallerClass directly need to manipulate SmallerClass as-is
EDIT:
I'm making a Collider class which is more or less a wrapper class around a Box2D body. I'm working with other people, and I want them to avoid touching the b2Vec2 class whenever possible, in favour of a custom vector class that we're using. With the Collider doing conversions between b2Vec2s and our vectors to maintain consistency.
I didn't make this vague to be annoying, I was just trying to generalise it so that I didn't get Box2D only answers and not be able to apply what I learned to a different problem
You're pretty much going to be limited to one of the following:
write a getter in BigClass that returns a reference to SmallClass (and call methods via that reference).
write small inline methods in BigClass that thunk the call into SmallClass (probably the simplest and best solution).
Use inheritance (which may or maynot be a good idea, depending on scenario)

Helper function: static in cpp or define as static helper class method [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I need to write a helper function(s) that will be used in a class method. Target is a static library. I know the following about the usage:
it will not use this class members
it will not be used in other classes
I see two general ways to resolve it:
define a helper class with static method(s) and use it
define it as a static function in cpp file
In the first case (as far as I understand) these functions will be visible in library symbols list. In the second case they will not be visible but it doesn't seem to be a good practice. Which way should I choose?
"helper class with static method(s)" is a Java-ism that has no place in C++.
A static function at file scope is a C-ism.
The modern C++ solution (only a couple of decades old) that you should choose is a free function in an anonymous namespace:
namespace
{
void helperfunction() {}
}
void Class::function()
{
helperfunction();
}

C++ abstract or interface classes? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I found this question: When to use abstract class or interface?. But it was asked for Java and C++ is different. There is multiple inheritence so maybe the answers are different too.
When shall I use an interface class ?
If I use the PIMPL idiom then there will be only one member which I only need to forward declare. And if I move the private functions into the PIMPL class then there will be only public and protected functions in a abstract class. So the difference between an abstract class like this and an ˙interface class is that in the interface class there shall be only pure virtual functions. Does it have any advantages over the previously mentioned one ?
Use an interface class when the class hierarchy can be viewed as generic; child classes can be swapped without affecting the calling classes.
For example, there is an std::istream class. Any function or method can treat input as generic if it requires an std::istream. So, one can pass cin or an ifstream to the function. The interface is consistent.
Use the PIMPL idiom when you want to hide the implementation from the interface. Used in library classes.

what is this requirement telling me not to do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Edit: I was only asking for a clarification on what my professor was requiring of me, I now understand what this requirement is saying and I believe that this has been thoroughly answered.
I have a project that i have to do for my c++ class and this is a requirement of the project.
Except for the print menu function, all user defined
functions(functions written by the programmer) must be called from
main. No user defined functions other than the print menu function can
be called from other user defined functions.
It is a strange requirement. During your homework you might write some functions. Or you might not, and your main() will be one unreadable big pile of lines that only you can read and understand, on a good day.
But, you can decide to pretify your code by moving some code from main() to some newly introduced functions. One function to read all data from input, one function to do something with that data, one function to write result to output. Such kind of things.
The requirement demands that all functions must be called only in main(). You must not call any function from any other function except main().
I guess this requirement makes it easy for the teacher to read your code.
That means you cannot nest functions, like this function definition
void A(){
int B();
}
then you are only able to call your functions from main()

Avoiding the windows header [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using both win32 and directx when making a game engine + game. Win32 directly needs the window header and directx header includes it aswell.
It is a decently-sized project and I'd rather avoid exposing the windows header to the rest of the project(s) if possible. Is there any good way to bypass this issue?
There's only one practical way to avoid exposing a header that your implementation code needs, and that's to include the header only in the implementation.
That's called the compiler firewall idiom.
Often (but this is not always required) one exposes a class with a member pointer to an object of declared but incomplete type, that is defined only in the implementation file. This is usually called the PIMPL, short for pointer to implementation, idiom, but it has also been called the handle-body idiom and the Cheshire Cat idiom.
One alternative to a PIMPL pointer is to declare a factory function in the header file, where that factory function produces objects of some known type Base, and in the implementation file let it produce an object of a derived type Derived that contains the dependencies on the undesirable header.
Another alternative, but then you might run into thread safety issues, is to just provide functions that operate on a static state variable. I.e. a singleton, a global. Yes this is as bad as it sounds (even though a whole programming language, Modula-2, was based on the idea), but it certainly is a technical option, and might be preferable in certain situations.