Page 1 of 1
Doubt, Confused with Hello World
Posted: Tue May 05, 2020 7:33 pm
by Nick
I'm still having trouble understanding and how I should declare my objects.
I simplified the Hello World example below to explain what I don't agree with it!
1) I didn't like the Frame Constructor staying below the "bool CMyProgram::OnInit() {". Then why do they do it like this?
2) I didn't like declaring the wxFrame Class and Contructor separate. Then why do they do it like this?
3) And what bothered me the most, declaring it that way leaves the binary twice the size!
Hello World Model:
Code: Select all
#include <wx/app.h>
#include <wx/frame.h>
class CMyProgram: public wxApp {
public:
virtual bool OnInit();
};
class CFrmMain: public wxFrame {
public:
CFrmMain();
};
wxIMPLEMENT_APP(CMyProgram);
bool CMyProgram::OnInit() {
CFrmMain *FrmMain = new CFrmMain();
FrmMain->Show();
return true;
}
CFrmMain::CFrmMain(): wxFrame(NULL, wxID_ANY, "") {
}
A model that during this time of learning I ended up creating:
Code: Select all
#include <wx/app.h>
#include <wx/frame.h>
class MyProgram: public wxApp {
bool OnInit() {
wxFrame *FrmMain = new wxFrame(NULL, wxID_ANY, "");
FrmMain->Show();
return true;
}
};
wxIMPLEMENT_APP(MyProgram);
I wish someone could guide me so that I know if the model I created to use is correct, or if I created a model that I should not use! And I want to understand why?
Re: Doubt, Confused with Hello World
Posted: Tue May 05, 2020 7:39 pm
by PB
Your model is generally wrong.
C++ way is to have declarations and definitions separate, unlike e.g. Java or C#. In C++, the declarations are supposed to be in header files (.h) and definitions in source files (.cpp)[1]. If the header files actually included the code, the compile times for all projects but tiny ones would be unacceptable. Source files also generally change less frequently then header files, changing a header file triggers rebuild for every file including that header. Non-Open-Source also ships only the headers with interface definitions and not actual code which the code owners may not want to make public (the actual code is then shipped in binaries, either static libraries or DLLs).
[1] The only exceptions are inline function, which are very short functions not being compiled and linked as separate functions but their binary code is inserted to a place from which they are called (if possible).
Last but not least, having the all definitions inlined could increase the executable size (if the compiler actually decides to inline them and the code is called from multiple places) but not vice versa.
Nick, I would suggest to read a good C++ primer where these very basic things are properly explained.
Re: Doubt, Confused with Hello World
Posted: Tue May 05, 2020 9:25 pm
by ONEEYEMAN
Hi,
Or even better - sign up for a college that have a class about C++ programming with a good teacher. (Not a JAVA-SUCKS language and not any other OOP language).
That way you will learn the language before doing any serious stuff with it.
No, in terms of your code - it might be fine for a small educational program, but as PB suggested you should start writing your code right from the very beginning.
Thank you.
Re: Doubt, Confused with Hello World
Posted: Tue May 05, 2020 10:34 pm
by Nick
PB wrote: ↑Tue May 05, 2020 7:39 pm
Your model is generally wrong.
Thank you for your answer, I understand your point of view! In my case then, I could ignore these options!
At the moment, I have created small programs just for my use. I have no intention of creating a commercial application now, and maybe I won't even do it later!
In the case of the Hello World Example then, I can understand what examples need to follow a business standard for creating professional Software!
I think what confused me was forgetting that the example is made for a professional application. Which was never my case at the time!
Re: Doubt, Confused with Hello World
Posted: Tue May 05, 2020 10:40 pm
by Nick
ONEEYEMAN wrote: ↑Tue May 05, 2020 9:25 pm
Hi,
Or even better - sign up for a college that have a class about C++ programming with a good teacher. (Not a JAVA-SUCKS language and not any other OOP language).
That way you will learn the language before doing any serious stuff with it.
No, in terms of your code - it might be fine for a small educational program, but as PB suggested you should start writing your code right from the very beginning.
Thank you.
It would be great if I could really do that! But unfortunately I can't afford it. My main focus nowadays is to develop in pure C, and I'm still studying C, step by step, researching on the Internet and counting on the help of forums.
I use wxWidgets to create the windows and controls, because it's easier, because in C I'm not even close to that level.
I know wxWidgets is in C++ but I didn't even start studying C++ so as not to get confused about C.
So all I write is in C, and when it's not possible, I use wxWidgets.
Re: Doubt, Confused with Hello World
Posted: Tue May 05, 2020 11:28 pm
by ONEEYEMAN
Hi,
I am sure there is a community college where you can sign up for a class with the help of the federal tuition program or something like that (Whatever is available in you country/state).
Its going to be very hard for you to write something (especially in C).
Thank you.
Re: Doubt, Confused with Hello World
Posted: Wed May 06, 2020 9:23 am
by PB
I still think that doing that is a bad idea, regardless of the assumed source code size.
There is a good reason that people who know what they are doing do not write their programs like that.
Re: Doubt, Confused with Hello World
Posted: Wed May 06, 2020 3:11 pm
by Nick
PB wrote: ↑Wed May 06, 2020 9:23 am
I still think that doing that is a bad idea, regardless of the assumed source code size.
There is a good reason that people who know what they are doing do not write their programs like that.
Thanks! Then I will have to re-read everything, re-study everything you told me again to really understand what you are trying to guide me and alert me. I will pay special attention to that!
Re: Doubt, Confused with Hello World
Posted: Wed May 06, 2020 3:25 pm
by PB
That's a good attitude!
I do understand (and remember), that one wants to write the code doing things ASAP and not read something. However, having a good idea about what I am doing can save so much time, effort, and spare from frustration in the future.
Re: Doubt, Confused with Hello World
Posted: Wed May 06, 2020 3:39 pm
by ONEEYEMAN
Hi,
This should be a structure of a C++ (and C for that matter) application:
Code: Select all
class Foo
{
public:
Foo();
~Foo();
void Bar();
int Baz();
private:
int x;
double y;
};
That code goes into the header file (let's say header.h.
Code: Select all
#include "header.h"
Foo::Foo()
{
// some initialization code goes here
}
Foo::~Foo()
{
// memory release and other clean-up goes here
}
void Foo::Bar()
{
// some operations
}
int Foo::Baz()
{
// some operations
}
This is typical source.cpp file.
As you can see the definition and implementation are split between 2 files.
Thank you.
Re: Doubt, Confused with Hello World
Posted: Wed May 06, 2020 4:53 pm
by Nick
Thank you all for all this lesson and clarifications
Re: Doubt, Confused with Hello World
Posted: Wed May 06, 2020 6:38 pm
by ONEEYEMAN
Hi,
That's why I suggested to go to school where you will learn the language basics and then it will be easier to work with the library such as wxWidgets.
Because this will happen every time. But most importantly - you will write much better code this way.
Thank you.