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 5 years ago.
Improve this question
Hello i want to create my own public c++ tool like auto socket connection. but i have two ways and i want to know which way is better.
way 1 :
insert the functions (with source) inside one header file and other people just include the header file and use the functions...
sock.h
#include <iostream>
class sock {
public:
bool create();
bool bind();
bool listen();
}
inline bool sock::create() { ... }
inline bool sock::bind() { ... }
inline bool sock::listen() { ... }
and second way is create a header file and also create static lib (c++)
sock.h
#include <iostream>
class sock {
public:
bool create();
bool bind();
bool listen();
}
sock.cpp
#include "sock.h"
bool sock::create() { ... }
bool sock::bind() { ... }
bool sock::listen() { ... }
which way is better ? ( i choose the first way myself because it's easy for client to just include one header file and use the functions but in second way client must include the header and also include the lib file. is there any problem for first way?
Functionally there is no problem. But by keeping the function definitions inside the header, every time the header is imported into a source file, all of the definitions will be compiled again, negatively impacting both compile time and binary size (in certain cases).
Best practice is to define only once, forward declare wherever possible. Keep your abstractions water tight.
Unless you're using template classes, in which case header definitions are generally required.
Related
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 2 years ago.
Improve this question
I can understand that headers are used to include classes and functions into another file, but .cpp just seem a bit useless to me.
.h files are used to declare functions that can be exported/imported, however, template functions and classes must be defined in the same .h file and not only declared.
I have only used .cpp files as of now for the main program, but can they be used for any other thing? I am using .h files for everything and just .cpp files for the int main() function
Question: What are the cases where you use .cpp files over .h files
Here's an example of how to organize your code. Define headers for any classes you're using, like:
// example.h
#pragma once // Avoid duplicate imports, if supported by your compiler
#include <string>
class Example {
public:
Example();
Example(const char* name);
~Example();
protected:
std::string name;
};
Then implement it:
#include "example.h"
Example::Example() {
}
Example::Example(const char* name_) : name(name_) {
}
Example::~Example() {
}
Then in main.cpp:
#include "example"
int main() {
Example e("Some Name");
return 0;
}
This will all change for the better when modules become the best way to declare things, but until then, this is how it's typically done.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
enter image description here
if I want to call use the value of char slide_piece in another cpp file. How do i implement the header file and how to call the function? Thanks
GameLogic is a class and slide_piece should be a member of it. Than you have to add a getter function:
// GameLogic.h
class GameLogic
{
public:
char get_slide_piece() // getter function
{
return m_slide_piece;
}
// The rest of the class:
bool MouseDown( char file, char rank, wxPoint &point ); // Sets m_slide_piece value
// & more
protected:
char m_slide_piece = '\0';
// I use to add to member-variable names: "m_"
// So I suggest to rename it to m_slide_piece
// to make it easier to realize when you read the
// code in the cpp that it is a member-variable.
// The rest of the class
};
In the other cpp file, access it by:
char slide_piece = game_logic.get_slide_piece(); // game_logic is a GameLogic object
In your example, slide_piece is a variable of MouseDown function and it is not valid after the function returned. The use of global variables is not a good practice.
Starting with C++17, you can declare and define your constant in a header using inline:
inline constexpr char slide_piece = '\42';
Which is the easiest and cleanest solution for that problem IMHO.
Declare it in header file as extern:
MyHeader.h:
extern char slide_piece;
Define it in Source file: (including MyHeader.h is not necessary, but it's good practice)
MySource.cpp:
#include "MyHeader.h"
char slide_piece;
Use it in any other source by including the header:
OtherSource.cpp:
#include "MyHeader.h"
// slide_piece is available here
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 5 years ago.
Improve this question
I researched this question on Google and Stak Overflow but couldn't find an answer to it.
I am trying to find out what all information is stored in a .cpp file extension. Meaning, is it just code that has been compiled (meaning compiled code)? Does have an object file in it? Does it include an object in it? What exactly does it consist of?
The .h usually has the class definition (code)
#ifndef CLASS_T_H
#define CLASS_T_H
class class_t {
public:
class_t();
class_t(const class_t& o);
~class_t();
class_t& operator=(const class_t& o);
private:
};
#endif
And the .cpp usually has the class implementation (code)
#include "class_t.h"
class_t::class_t() {
}
class_t::class_t(const class_t& o) {
}
class_t::~class_t() {
}
class_t& class_t::operator=(const class_t& o) {
return *this;
}
You could use the class_t in another .cpp file by inclduing the .h file and compiling the cpp files into a binary executable. One of the cpp files would contain your main() method.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
i'm new to c++ (i had some time with Java and C before)
i can't get it... eclipse is creating header and cpp files when i want to create some new class, why ? thing is , it seems like the header includes the class basic implantation so what does the cpp file is being used ?
header:
#ifndef HEAP_H_
#define HEAP_H_
namespace std {
class Heap {
public:
Heap();
virtual ~Heap();
};
} /* namespace std */
#endif /* HEAP_H_ */
cpp:
#include "Heap.h"
namespace std {
Heap::Heap() {
// TODO Auto-generated constructor stub
}
Heap::~Heap() {
// TODO Auto-generated destructor stub
}
} /* namespace std */
the object oriented thinking in c++ just seems weird. what's the point of an header file when you have a class file with methods ?
anyway , thx ahead!
The header file is where the declaration of the class is. This is done so that multiple files are able to include the Heap class.
You COULD just write the implementation in the header file by itself, but then you wouldn't be able to include it in more than one file due to the method/class/whatever being declared multiple times. This is actually true in C as well. If you create an include file:
int double_me(int number) {
return number * 2;
}
And then include that in multiple files, the compiler will complain about the same method being declared multiple times.
That being said, the header file is where all the class functions and variables are declared. The .cpp source file is where the implementation is written.
For instance, if you wanted to add a method to the Heap class:
Heap.h:
#ifndef HEAP_H_
#define HEAP_H_
namespace std {
class Heap {
public:
Heap();
virtual ~Heap();
void do_nothing(int some_parameter);
};
} /* namespace std */
#endif /* HEAP_H_ */
Heap.cpp:
#include "Heap.h"
namespace std {
Heap::Heap() {
// TODO Auto-generated constructor stub
}
Heap::~Heap() {
// TODO Auto-generated destructor stub
}
void Heap::do_nothing(int some_parameter) {
}
Another advantage is for reduced compile times. If a recompile was to take place, but the Heap object file was not changed, then it won't have to recompile the Heap implementation.
It is pretty standard for a C++ class to have both a header file and an implementation file. The reason for this is that it is common for other functions which use the class to need to know the way in which to make calls to the class (found in the header file), while they don't need to know how those calls are actually implemented. The only functions that are implemented in a header file should be inline (which get moved into the calling function), or template functions.
In fact, if the compiler implements the same function multiple times into multiple object (*.o) files, it will come back with an error during linking. This is the same as it is with C implementation files header files. The header file contains the prototypes, and the implementation file contains the, well, implementation.
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 9 years ago.
Improve this question
Take the following example:
// base.h
#include <string>
struct base
{
virtual ~base() = default;
virtual void do_something(const std::string& arg) const = 0;
};
// derived.h
struct derived : base
{
void do_something(const std::string& arg) const
{
//...
}
};
In this example should derived.h include the string header?
I fully agree with the principle of including what you use but in this case base.h has to include string and if the interface changes to not use string (and the include <string> is accordingly removed from base.h) then the interface will break highlighting anyways.
If the interface changes not to include string, your derived do_something will have to change as well. If you need in the derived.h independently of the interface (e.g. due to details unrelated to interface but present in the implementation) - in this case yes, it's valid and probably preferable to include <string> there as well.
I take the approach of always includeing anything I need for that file. I then don't have to go around hunting through headers and untangling webs of dependencies when I'm missing a symbol somewhere. Include guards mean that this is cheap.
But it's completely up to you. If you want to omit it, to make derived.cpp shorter, and add it in at a later date if the need arises, that's fine too.
There's simply no "right" answer.
You can include "base.h". Don't think too much.The less code you write,the better your code is.
#include "base.h"
struct derived :public base
{
void do_something(const std::string& arg) const
{
//...
}
};