Hi
I'm just trying to break up my program and am unsure how to handle a struct in c++. I have a function I am calling which may or may not be in the same file as the main function.
I have one source file and a header file. I want to break up the structure but putting it's definition in the source.cpp file and a declaration in the source.h file but I can't seem to get this to work.
What is the conventional way, or how should I structure this? I definitely want to break things up as much as possible.
Thanks
source.h
1 2 3 4 5 6
#ifndef SOURCE_H_INCLUDED
#define SOURCE_H_INCLUDED
// ? I AM NOT SURE WHAT GOES IN HERE?
#endif // SOURCE_H_INCLUDED
Why the void* and C-style cast? What if I call the myfunction with int*?
The question is: Who has to know what?
The main() has to reserve space for object ss, so it has to know the size of mystruct.
The main() has to know that myfunction() exists.
The myfunction() has to dereference the members of mystruct and thus know about them.
The mystruct could have a constructor. It would need to know about mystruct.
mystruct.h
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
#include <string>
using std::string;
struct mystruct
{
string str;
string astr;
int myint;
mystruct( string, string, int );
};
#endif
mystruct.cpp
1 2 3 4 5 6
#include <utility>
#include "mystruct.h"
mystruct::mystruct( string a, string, b, int c )
: str( std::move(a) ), astr( std::move(b) ), myint( c )
{}
That's an excellent example. Thank you very much.
I used the void cast for something else so I wanted to keep it. That's the easy part, it was breaking everything up which became a bit confusing.