473,837 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class method less robust than C function?

I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).

Example 1:

class MyProg
{
public:

void Run()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
}
};

int main()
{
MyProg p;
p.Run(); // just doesn't feel right to run the whole prog in a class
func
return 1;
}

Example 2:

int main()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
return 1;
}

What I'm thinking about is whether a class is more likely to be corrupted
than the equivalent C code. Perhaps if C++ went "all the way" and eliminated
the C main function I'd feel better with the class-encapsulated message
loop.
As it is though, even C++ likes procedural main rather than some kind of
"program" class.

Please feel free to add any comments on tangential but associated topics
also.

Tony
Nov 14 '06 #1
43 2108
Tony wrote:
I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).
Does anything in MFC feel right?
>
What I'm thinking about is whether a class is more likely to be corrupted
than the equivalent C code. Perhaps if C++ went "all the way" and eliminated
the C main function I'd feel better with the class-encapsulated message
loop.
Corrupted by what?

--
Ian Collins.
Nov 14 '06 #2

Tony wrote:
I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).
MFC is off topic here as well as anything to do with "messaging" . An
MFC class is not a C++ class either. The rule of thumb is: if it only
runs in Windows or only in Linux or whatever OS, its off topic here.
The language dealth with here is the C++ standard library.

Do yourself a big favour and consider WTL instead. What MFC should have
been in the first place.
It supports templates and inheritance. Its too bad that MS dropped
support for it, cause it rocks when compared to MFC. Last i checked, MS
remitted it to OpenSource under a dual-license.
http://en.wikipedia.org/wiki/Windows_Template_Library
>
Example 1:

class MyProg
{
public:

void Run()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
}
};

int main()
{
MyProg p;
p.Run(); // just doesn't feel right to run the whole prog in a class
func
return 1;
}

Example 2:

int main()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
return 1;
}

What I'm thinking about is whether a class is more likely to be corrupted
than the equivalent C code. Perhaps if C++ went "all the way" and eliminated
the C main function I'd feel better with the class-encapsulated message
loop.
As it is though, even C++ likes procedural main rather than some kind of
"program" class.

Please feel free to add any comments on tangential but associated topics
also.

Tony
Nov 14 '06 #3

"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.com...
>
Tony wrote:
>I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).

MFC is off topic here as well as anything to do with "messaging" . An
MFC class is not a C++ class either. The rule of thumb is: if it only
runs in Windows or only in Linux or whatever OS, its off topic here.
The language dealth with here is the C++ standard library.
It wasn't an MFC question at all. MFC was just the example. You clipped
too much of the original post that gave the context.

Tony
Nov 14 '06 #4

"Ian Collins" <ia******@hotma il.comwrote in message
news:4r******** *****@mid.indiv idual.net...
Tony wrote:
>I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).
Does anything in MFC feel right?
That doesn't add anything.
>What I'm thinking about is whether a class is more likely to be corrupted
than the equivalent C code. Perhaps if C++ went "all the way" and
eliminated
the C main function I'd feel better with the class-encapsulated message
loop.
Corrupted by what?
I don't know. I'm not a compiler writer. That's why I posted the question!
Perhaps
a guru answer would have addressed the issues surrounding combining data and
function and if that affects code robustness and how.

Tony
Nov 14 '06 #5
Tony wrote:
I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).
I acknowledge that it is not an MFC-exclusive question. But this isn't a
C++ language question either. This is all about library design.
>
Example 1:

class MyProg
{
public:

void Run()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
}
};

int main()
{
MyProg p;
p.Run(); // just doesn't feel right to run the whole prog in a class
func
return 1;
}
Example 1 is clumsy. But this has nothing to do with C++. Go ask in an
MFC newsgroup why MFC handles messages like the above (although I
believe MFC was trying to hide both main()/WinMain() and the message
pumping loop from the user.)
>
Example 2:

int main()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
return 1;
}
No comment.
>
What I'm thinking about is whether a class is more likely to be corrupted
than the equivalent C code. Perhaps if C++ went "all the way" and eliminated
the C main function I'd feel better with the class-encapsulated message
loop.
Again MFC's design philosophy isn't exactly what C++ is.
As it is though, even C++ likes procedural main rather than some kind of
"program" class.
What do you mean? C++ IS procedural, depending how you define "procedural ".
>
Please feel free to add any comments on tangential but associated topics
also.
This post can be better responded in another newsgroup.
>
Tony

Ben
Nov 14 '06 #6

OT but ...

Every gui toolkit has something very similar class representing the
application and encapsulating the message loop

wxWidgets -wxApp
QT -QApplication
VCL -TApplication
and many many more...

All of them have a Run() method.

Its the way things are done....
And I dont get whats less "robust" about this.

Nov 14 '06 #7

Tony wrote:
"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.com...

Tony wrote:
I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).
MFC is off topic here as well as anything to do with "messaging" . An
MFC class is not a C++ class either. The rule of thumb is: if it only
runs in Windows or only in Linux or whatever OS, its off topic here.
The language dealth with here is the C++ standard library.

It wasn't an MFC question at all. MFC was just the example. You clipped
too much of the original post that gave the context.

Tony
I didn't clip anything, and i can perhaps mention the following.
Although it is OT.

MyProg is basicly the equivalent of an application struct, which has
the capacity of storing handles to windows. Remember that a window is
not neccessarily a visible frame. A window is a targetable msg reciever
(it has a handle). So in the event that you need to send a message
directly to a window (as opposed to posting messages throughout the
window hierarchy), you'ld rather have Run() loop away the messages and
reroute with MyProg object's stored handles if you choose to. Also, in
the event you chose to spawn a thread to "run" the program, again using
the Run() member function will leave you with that option.

Now, as far as class robustness is concerned. How you protect your
encapsulated members and how you apply your const qualifiers is what
will decide if the class is robust. Needless to say, const is crucial
in C++ at every level. Any member function that is non-static and does
not modify the class should be const ( void MyProg::foo() const ). Any
member that is not volatile should be const. Parameters should
preferably be const references (something MFC balks at more often than
not). Whether you Run() in MyProg or loop in main() isn't going to
change those needs.

The problem here is that you are using MFC which is really C with a
pseudo-virtual mechanism with old style C function overrides and
pointers galore, which means that underneath all those overriden
functions, there is a implementation running that is hidden from you
and which you really can't do much about.

C++ classes tend to be much simpler in design than MFC "classes" with
security as the primary objective. You can't even pass void* around.
Its typical to program thousands of lines without ever using a single
pointer (except perhaps for smart pointers). So to disscuss robustness
with an implementation like MFC, which is nothing *but* loose pointers
is an oxymoron.

Here is a good example. Consider something ridiculous like:
int n;
const int* p_n = &n;
Is that a safe pointer? no its not, the value at the pointer is const
but the pointer itself can be changed!!!
int const * const p_n(&n); // ah !!!
p_n is now a constant pointer to a const integer. Yet nowhere will you
see that in MFC, which is a shame. That const pointer is nuke proof
until destruction - read it is all you can do. It basicly behaves like
a const reference after its initialized.

Use Run() since its static anyways (does not have a *this to the
object). You can let main() deal with exceptions, for example. What
i'ld suggest is learn how to encapsulate the class members and
functions. Thats always worth the trouble.

Nov 14 '06 #8

"Tony" <rd************ **@sbcglobal.ne twrote in message
news:Z0******** **********@news svr12.news.prod igy.com...
I'm working with GUI messaging and note that MFC encapsulates
the message loop inside of a C++ class member function. Is this
somehow inherently less robust than calling the message loop
functions within main? (It just doesn't "feel" right to me).
"Less" robust? Sounds more robust to me. It hides the implementation
details inside the class, so if the system requirements change, the change
can be made inside the class itself. Pretty common, if you ask me.
Example 1:

class MyProg
{
public:

void Run()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
}
};

int main()
{
MyProg p;
p.Run(); // just doesn't feel right to run the whole prog in a class
func
Why not?
return 1;
}

Example 2:

int main()
{
Msg m;
while(GetMsg(m) )
DispatchMsg(m);
return 1;
}

What I'm thinking about is whether a class is more likely to be corrupted
than the equivalent C code.
Corrupted by what? I don't understand the question. Classes are the heart
of object-oriented programming. If they were less stable or reliable than
procedural code, nobody would use them, and C++ would vanish.
Perhaps if C++ went "all the way" and eliminated
the C main function I'd feel better with the class-encapsulated message
loop.
As it is though, even C++ likes procedural main rather than some kind of
"program" class.
What do you mean that C++ "likes procedural main"? The language couldn't
care less whether you do all your work in main or in global functions or in
classes.

It sounds as if you're suggesting removing main in favor of some kind of
global application class here. But how would other languages interact with
such a C++ class?

I think you're just worrying about nothing.

-Howard

Nov 14 '06 #9
Howard wrote:
"Tony" <rd************ **@sbcglobal.ne twrote in message
news:Z0******** **********@news svr12.news.prod igy.com...
>I'm working with GUI messaging and note that MFC encapsulates
[..]
[..]
I think you're just worrying about nothing.
I think he's just trolling.
Nov 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
1688
by: RageARC | last post by:
I have the following code: index.php: class main_class { public $database = new DAL; public $html = new HTML; }
0
9852
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10902
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9420
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.