I am trying to get public struct to whole project which data should be accessed from everywhere like is possible in C, but have problems to do that.
It is possible to do exactly like it was possible in C. Code you have shown will not work in C.
I suppose you want global variable of type _ini. If ypu want an OO approach, you probably should look into Singletone pattern: it was created to make sure tht there could be only one global accessible variable.
Also I suggest to read more about variables, their visibility and storage duration. You clearly have problems with that.
But after returning from connectiondata I havent data in ini structure.
That's because the ini variable inside the function is local to that function - it doesn't affect any other variables elsewhere.
You could send a reference to a object of type _ini to the connectiondata function - then it's data will be changed.
How to achieve that in general, if possible out of classes?
Sure you can do this with classes - have a read of the classes section of the tutorial on this site (top left of this page) . If you do this correctly - with a separate header file & .cpp file, you can include the header file into whichever .cpp file needs to use it.
If you have a class - then the connectiondata function could be a constructor & the DBconnect function a member of the class, the variables it uses should be private data members of the class.
Btw, did you intend for the DBconnect function to be recursive?
You also need a constructor & public functions which make up the interface for the class. Don't be tempted to have a get / set function for every member of the class - particularly set functions which are straight assignment are very bad news.
// struct _ini ini; // *** change this; it is a definition of an object with external linkage
typedefstruct _ini ini; // this is probably what you want to do.
And change the name _ini to (say) connection_data
We should not be having any name beginning with a leading underscore at global scope.
The problem is that object ini was defined twice. The first its definition is in the file where you defined function connectiondata because you included the header with the object definition in this file. The second its definition is in the file where there is main function because you again included the same header file.
Some linkers when they see two definitions of the same object with external linkage convert such an object as having internal linkage. As the result you get two objects of the same type and with the same name that are in fact different and are not visible outside compilation units where they are defined.
So function connectiondata changes the object that is in the compilation unit where there is the definition of the function. At the same time the other object with the same name that is in main was not changed.
To correct the situation you should write in the header
extern struct _ini ini;
and in the module with the function definition of connectiondata write
JLBorges, that one works! Thank you very much:)
struct cdat ini = connectiondata();
Vlad, sorry, this is my starting touch with C++, but I have some experience with C so I am included header as needed in both files.
I declared local structure in function by mistake and ununderstanding.
Now I am repair example by your advices and it is shown above, (I repair previous post).
It compiles OK but don't transfer data. What more can I do to get it working?
That from main I can use out of functions, at top of program and then ini is reachable allaround, just what I search for...
But when we takling...
If I use "namespace std" (like for strings), include C++ headers like <string> and assign one string like mystring = "mytry" then I get compiled exe about 430 kb (under release). Same program, if I use C strings is about 9.5 kb (exe).
Is this normal or I messed something with compiler options?
> If I use "namespace std" (like for strings), include C++ headers like <string>
> and assign one string like mystring = "mytry"
> then I get compiled exe about 430 kb (under release).
> Same program, if I use C strings is about 9.5 kb (exe).
> Is this normal
It is normal for a C++ program to have a larger memory footprint. For instance, the standard C++ streams are instantiated in addition to the standard C streams, there are a few more allocators than malloc() and friends, the implementation of the C++ portion of the library may call C library functions anyway, and so on. How much more depends on the implementation.
What platform do you intend to run this code on? Where 430 kb would be deemed to be extravagant?
I try this on windows and intend to try on linux also.
As unexperience in C++ I have to ask is this normal and common, nothing more.
What this brings in practice I will see later with advancing...
Thanks for help, informations and advices for all which try to help.
> I try this on windows and intend to try on linux also.
Then just ignore the difference in size - 400 kb is not something that one would give a second thought to on these platforms. Open Task Manager and check the memory usage of the programs that you regularly - the browser, IDE etc.
This program is tiny and 430 kb vs. 10 kb looks like a lot. For a somewhat bigger program, 5 MB vs. 5.5 MB wouldn't look all that different.
/* those variables WILL BE global */
externstruct person p;
extern std::string debugmode;
/* global functions */
#endif /* COMMON_H_*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* defining GLOBALS declared by extern */
struct person p;
/* END GLOBALS */
p.name = "marie";
p.age = 20;
debugmode = "yes";
Defining of globals can be in public area (NOT inside any function) of any file which includes common.h, but not in more than only one file!! That way structure p is accesible on all files which includes common.h.