Class method less robust than C function?

D

dustmop

Tony said:
That doesn't add anything.

C++ needs to maintain backwards compatability with C, at least to the
point that it can compile 99% of C programs out there. C had no
objects, thus it had a global main function as an entry point. So C++
has one too.

Note that when you're talking about frameworks, this requirement goes
away. Object-based frameworks cannot jive with C, so there's no need to
keep the old C linkage in place.
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.

The way you're talking about classes is a little silly. There no
meaning to "combining data and function", this concept only exists at
compile time. After it's all been compiled, data and code work pretty
much the same way they do in compiled C code. They live in their
separate areas of memory (segments / sections / pages / whatever) and
independantly do their own thing. Objects don't just spontaneously get
corrupted, if anything less memory corruption occurs in C++ due to
libraries with buffer checking.

If you're really interested in learning how it really works underneath
the hood, here's a good start: [
http://www.programmers-corner.com/tutorial/36 ]. Note that this is only
one possible implementation technique, and thus is compiler-specific /
environment-specific / os-specific / day-of-the-week-specific and
therefore undefined and off topic.
 
T

Tony

benben said:
Because putting the main function in a class just doesn't make any sense.
Why would you want to do that? And what do you get from doing that?

Just looking for an OO approach from the get go. It could give entire
program
specialization through inheretitance perhaps. As it stands frameworks fake
this approach so maybe some attempt to embrace it as fundamental would be
appropriate. It may also make programming more accessible by making things
easier to comprehend (no need to program in a multi-paradigmic way).
Still I don't see the question. What's wrong?


This has got nothing to do with low level implementation of C++. It is a
design question. C++ is designed to have a standalone main() function as
an entry point and programs written in C++ adapts to that fact.

It's directly associated with the design and implementation of C++ and
perhaps
its evolution.

Tony
 
T

Tony

C++ needs to maintain backwards compatability with C, at least to the
point that it can compile 99% of C programs out there. C had no
objects, thus it had a global main function as an entry point. So C++
has one too.

I could have an alternative paradigm also though. One more aligned with
the OO side of things. C++ a multi-paradigm language? Yes. But can one
program to one paradigm or the other exclusively? Well apparently not
OO from the get go surely (main() required, and nothing OO about that).
Note that when you're talking about frameworks, this requirement goes
away. Object-based frameworks cannot jive with C, so there's no need to
keep the old C linkage in place.


The way you're talking about classes is a little silly. There no
meaning to "combining data and function", this concept only exists at
compile time.

Well there's a lot more going on under the hood, vtables and other stuff.
So just the fact that it is more complex under the hood may have some
relevance.
After it's all been compiled, data and code work pretty
much the same way they do in compiled C code. They live in their
separate areas of memory (segments / sections / pages / whatever) and
independantly do their own thing. Objects don't just spontaneously get
corrupted, if anything less memory corruption occurs in C++ due to
libraries with buffer checking.

If you're really interested in learning how it really works underneath
the hood, here's a good start: [
http://www.programmers-corner.com/tutorial/36 ]. Note that this is only
one possible implementation technique, and thus is compiler-specific /
environment-specific / os-specific / day-of-the-week-specific and
therefore undefined and off topic.

I'd actually be more interested in knowing why my example 2 is preferred
by most over example 1 (from what has been said, MFC makes its Run()
function static). Heck, why not make a Program class and put the Run()
functionality (a message loop for EXAMPLE) in the constructor and do
clean up in the destructor?

Tony
 
T

Tony

Frederick Gotham said:
Tony:



As you know, in C++, we try to make our code as inefficient as possible.
No,
actually, we try to make our code look as "modern" as possible, and one
side-
effect is that it comes out inefficient.

Of course, to make our code even more modern, we'll have to start using:

class Program {
public:

static int main()
{

}
};

Then we'll just be a small step away from Java.

Perhaps the perceived inefficiency of doing the Program::Run() thing is why
it's not a low level C++ construct.

Tony
 
T

Tony

Steve Pope said:
Firstly, the question is perfectly on topic.

The answer is that neither is more robust, however it is usually
poor practice to create an object just to call a member function,
which is what is being done here. There is no object-oriented
functionality; the member function is being called just for its side
effects, it does not manipulate any data members of the object.

Surely a real class would have data members to "manipulate" though.
It's just an example to point out the corresponding paradigms.
In this situation, a much better approach is to use a plain (global)
function, and if necessary put it in a namespace to achieve
encapsulation.

Tony
 
T

Tony

Alan Johnson said:
Because there is no reason to do it that way. C++ is a multi-paradigm
language.

Well the casual observer would think that means that one can program
exclusively in one paradigm. Unfortunately in C++ that is not true. One
MUST program multi-paradigmically in C++.
If the best solution to a problem is object oriented, then
C++ can handle that.

Not exclusively. Not from the get go.
If the best solution is procedural, then C++ can
handle that too.

Granted, that it can. It's slanted toward procedural rather than OO (if one
weighs the program entry point more heavily perhaps).
C++ even has a few functional elements (especially if
you consider TMP). With the way things currently are, if a programmer
wants to model his entire application as an object, C++ makes it
trivially easy to do that.

Actually, it doesn't! (main() is required and it is procedural).
A more pragmatic reason might be that there is some effort spent on
trying to maintain compatibility with C. If you were forced to model
an application as an object, then C programs could no longer be
compiled with a C++ compiler.

Well no one was suggesting forcing anything. Currently one is forced to
program multi-paradigmically. Adding an alternative OO approach that
replaces main() at a low level (not just wrapping it) would allow
an OO approach exclusive of the procedural paradigm (and probably
open up a number of other possibilities).

Tony
 
T

Tony

red floyd said:
Actually, I've seen (in the early days), programs that did exactly that.

Some primitive CUI frameworks actually did all the work out of the
constuctor for a global "App" object, and main() was an empty function.

And that thought of course is where this thread started.

Tony
 
S

Steve Pope

Surely a real class would have data members to "manipulate" though.
It's just an example to point out the corresponding paradigms.

I agree, but then the example is lacking in essential details. I can
only comment on the code that was posted. A class does not
make sense in this case. "When to use a class" is an important
concept in C++.

Steve
 
D

dustmop

Tony said:
I could have an alternative paradigm also though. One more aligned with
the OO side of things. C++ a multi-paradigm language? Yes. But can one
program to one paradigm or the other exclusively? Well apparently not
OO from the get go surely (main() required, and nothing OO about that).

This has nothing to do with paradigms or exclusivity. The reason C++
programs begin running at a "main" function is historical. C++ was
originally just a front end preprocessor that spit out C code. It
wasn't designed as object-oriented from the ground-up. C++ is *not*
Java.
Note that when you're talking about frameworks, this requirement goes
away. Object-based frameworks cannot jive with C, so there's no need to
keep the old C linkage in place.


The way you're talking about classes is a little silly. There no
meaning to "combining data and function", this concept only exists at
compile time.

Well there's a lot more going on under the hood, vtables and other stuff.
So just the fact that it is more complex under the hood may have some
relevance.
After it's all been compiled, data and code work pretty
much the same way they do in compiled C code. They live in their
separate areas of memory (segments / sections / pages / whatever) and
independantly do their own thing. Objects don't just spontaneously get
corrupted, if anything less memory corruption occurs in C++ due to
libraries with buffer checking.

If you're really interested in learning how it really works underneath
the hood, here's a good start: [
http://www.programmers-corner.com/tutorial/36 ]. Note that this is only
one possible implementation technique, and thus is compiler-specific /
environment-specific / os-specific / day-of-the-week-specific and
therefore undefined and off topic.

I'd actually be more interested in knowing why my example 2 is preferred
by most over example 1 (from what has been said, MFC makes its Run()
function static). Heck, why not make a Program class and put the Run()
functionality (a message loop for EXAMPLE) in the constructor and do
clean up in the destructor?

Tony

I believe MFC is also doing a bunch of additional prologue and epilogue
code within the framework, before and after your "Run" function using
inheritance. But regardless, I'm sure we all agree MFC is just plain
silly.
 
T

Tony

Steve Pope said:
I agree, but then the example is lacking in essential details. I can
only comment on the code that was posted. A class does not
make sense in this case. "When to use a class" is an important
concept in C++.

Read the whole post/thread from the beginning and you'll comprehend
(hopefully) what the "issue" is. It's a high level C++ language design
and implementation question rather than a textbook programming puzzle.

Tony
 
T

Tony

This has nothing to do with paradigms or exclusivity. The reason C++
programs begin running at a "main" function is historical. C++ was
originally just a front end preprocessor that spit out C code. It
wasn't designed as object-oriented from the ground-up. C++ is *not*
Java.

I made a typo: 'I' should have been 'It'. Again, you are assuming that it
has to
be one or the other. Apparently you have a paradigm!

(Aside: 'paradigm' is by definition "bad". We (I) use it more loosely than
that
but indeed when one starts thinking outside the box in a programming
language
group, the term 'paradigm' seems real appropriate (!)).
I believe MFC is also doing a bunch of additional prologue and epilogue
code within the framework, before and after your "Run" function using
inheritance. But regardless, I'm sure we all agree MFC is just plain
silly.

I _had_ to use it 10 years ago on a project. I wouldn't touch it with a
10-ft
pole in my own work though. It's not that MFC is inherently bad, it's that
the underlying API is "bad"! (OK, so they could have chosen to abstract
the uglies rather than the "thin veneer" approach).

My question wasn't about MFC though.

Tony
 
H

Howard

I believe MFC is also doing a bunch of additional prologue and epilogue
code within the framework, before and after your "Run" function using
inheritance. But regardless, I'm sure we all agree MFC is just plain
silly.

What's silly is making such blanket statements. We certainly don't "all"
agree.

If you don't like it, fine. Don't use it. But let's try to keep the
discussions about the C++ language, and not about personal opinions on
third-party products.

-Howard
 
D

dustmop

Howard said:
What's silly is making such blanket statements. We certainly don't "all"
agree.

If you don't like it, fine. Don't use it. But let's try to keep the
discussions about the C++ language, and not about personal opinions on
third-party products.

-Howard

Sorry for the levity. I didn't mean to offend your personal tastes.
 
S

Steve Pope

Read the whole post/thread from the beginning and you'll comprehend
(hopefully) what the "issue" is.

I always read an entire thread before posting to it.

I was commenting on (1) your original question of whether or
not to use a class in this case and (2) Howard's comment that
it is better to use a class, due to the encapsulation; I disagree
with that (for the reason I described above).

You then went on to ask some other questions ("Why does C++ use
main()") which I chose not to address because the discussion
there was fairly complete. But since nobody had (yet) made
the observation that a namespace rather than a class is the
correct encapsulation method for the example given, I thought
I'd point that out.

Steve
 
B

benben

Frederick said:
Tony:



As you know, in C++, we try to make our code as inefficient as possible. No,
actually, we try to make our code look as "modern" as possible, and one side-
effect is that it comes out inefficient.

Of course, to make our code even more modern, we'll have to start using:

class Program {
public:

static int main()
{

}
};

Then we'll just be a small step away from Java.

Seriously, what's the difference? Your Program class is nothing but a
glorified plain main().

On the other hand, libraries, especially GUI libraries typically
provides a class that many people mistakenly suppose to represent the
whole program. And such a class typically provides member functions like
Run() or main() etc.

But we must see that the nature of all GUI application involves some
sort of event handling, which is typically done with querying events
from a loop. A good GUI library should provide an abstraction beyond the
detail of repeatedly query (or pumping) these events and the best way to
do it is to hide the looping code away from the user with the
application class.

And that has nothing to do with low level implementation of C++, nor the
C++ language per se.

Regards,
Ben
 
B

benben

Perhaps the perceived inefficiency of doing the Program::Run() thing is why
it's not a low level C++ construct.

Tony

What do you mean by "low level C++ construct"? A static class member
function is just as efficient as a standalone function. A non-static
class member is the same as the standalone counterpart with an extra
expense of passing and additional this pointer, which is minimum.

After a long post history I still don't quite get your intention. So
I'll just rephrase what I think your question is:

Why a C++ program's entry point is the global

int main();

instead of, say,

class C
{
int main(){}
};

?

If the above is indeed your questions then the answer is because the
former is cleaner, minimum, C-compatible, and can be used to facilitate
the latter.

Ben
 
T

Tony

benben said:
What do you mean by "low level C++ construct"?

Some kind of replacement for main().
A static class member function is just as efficient as a standalone
function. A non-static class member is the same as the standalone
counterpart with an extra expense of passing and additional this pointer,
which is minimum.

After a long post history I still don't quite get your intention. So I'll
just rephrase what I think your question is:

Why a C++ program's entry point is the global

int main();

instead of, say,

class C
{
int main(){}
};

?

If the above is indeed your questions then the answer is because the
former is cleaner, minimum, C-compatible, and can be used to facilitate
the latter.

The former is procedural though and probably hard to grasp for someone
learning how to program and using the OO paradigm. That's not the only
reason for looking for a better startup architecture: there's probably a
number of other things that an alternative program initiation/startup
would afford (don't ask me what those are, I just wish the compiler
and language designers would think about it some more. Perhaps ordered
initialization could be handled better, I dunno).

Your class C is "MFC-like" in that it just encapsulates the existing rather
than doin something new or more along the lines of OO application
frameworks. I know that classes are just things built up from C structs and
function pointers, but maybe viewing the whole program from an OO
perspective would yield some treasures. Again, not as a replacement for
the procedural C style, but as an additional alternative. Who knows, maybe
rearchitecting (thinking about it) would show that the C style is to be
"emulated" rather than the OO style. I just wonder about it, as I have no
intention of getting deep into compiler or hardware stuff.

Tony
 
T

Tony

benben said:
Seriously, what's the difference? Your Program class is nothing but a
glorified plain main().

Indeed. Since it is static, it's exactly the same. Nothing OO about that.
It's
an encapsulation of the idiosynchrasies of the C paradigm (emphasis on
'paradigm').
On the other hand, libraries, especially GUI libraries typically provides
a class that many people mistakenly suppose to represent the whole
program.

That's not necessarily bad. Should/could there be such semantics and can
those semantics reflect the machine and environment even better?
And such a class typically provides member functions like Run() or main()
etc.

But we must see that the nature of all GUI application involves some sort
of event handling, which is typically done with querying events from a
loop. A good GUI library should provide an abstraction beyond the detail
of repeatedly query (or pumping) these events and the best way to do it is
to hide the looping code away from the user with the application class.

And that has nothing to do with low level implementation of C++, nor the
C++ language per se.

Well it does in that it is one type of program. Perhaps a reanalysis of what
a program is, is in order (where now "program" means "C program").

Tony
 
B

benben

After a long post history I still don't quite get your intention. So I'll
The former is procedural though and probably hard to grasp for someone
learning how to program and using the OO paradigm.

This is not true. OO in its very nature is procedural. And don't forget
the design of C++ tried very hard NOT to force people thinking in any
certain paradigm in particular (such as OOP.) In addition,
C-compatibility is important therefore anything that easily breaks C
programs will not be considered.
That's not the only
reason for looking for a better startup architecture: there's probably a
number of other things that an alternative program initiation/startup
would afford (don't ask me what those are, I just wish the compiler
and language designers would think about it some more. Perhaps ordered
initialization could be handled better, I dunno).

Seriously, you can do all these in the plain old main() easily, and
elegantly. You can do an object-oriented program with main(). The
following program is perfectly fine on me:

#include <vector>
#include "shapes.hpp"

int main()
{
std::vector<shape*> v = load_shapes_from_file("shapes");
v.push_back(new triangle);
v.push_back(new rectangle);

for_each(v.begin(), v.end(), print_shape(window::main_window()));
for_each(v.begin(), v.end(), deallocate);

return 0;
}

If you find it hard to say the above is an OO program, you'd better rethink.
Your class C is "MFC-like" in that it just encapsulates the existing rather
than doin something new or more along the lines of OO application
frameworks. I know that classes are just things built up from C structs and
function pointers, but maybe viewing the whole program from an OO
perspective would yield some treasures.

Again, C++ is quite more than just a "pure OO language" in that it gives
you freedom to choose what you want. And OO isn't always the best way to
construct a program. And making main() a virtual member function doesn't
make you a good OO program anyway.

If you want an alternate entry point, you can easily achieve it like this:

/// chess_game.hpp ////////////////////////////
#include "all_my_includes.hpp"

#define ENTRY_CLASS chess_game
#define ENTRY_POINT start_chess_game

class chess_game
{
// all well designed OOP style stuff...
// and others

// entry point
virtual int start_chess_game()
{
// ...
}

};


// main.cpp //////////////////////////////////
#include "chess_game.hpp"

int main()
{
ENTRY_CLASS entry;
return entry.(ENTRY_POINT)();
}


I usually think of OO in terms of how it makes large scale project
manageable. The primary tool is designing libraries that are flexible
and well encapsulated. A standalone main() certainly doesn't stand the
way thereof.

In a lot of cases, better encapsulation means using more standalone
non-member functions since they are encapsulated from the class internals.
Again, not as a replacement for
the procedural C style, but as an additional alternative. Who knows, maybe
rearchitecting (thinking about it) would show that the C style is to be
"emulated" rather than the OO style. I just wonder about it, as I have no
intention of getting deep into compiler or hardware stuff.

Perhaps a little reading will make my point clearer:

http://www.artima.com/intv/goldilocks.html
http://www.artima.com/intv/modern.html
http://www.artima.com/intv/abstreffi.html
http://www.artima.com/intv/elegance.html

Those are very interesting interviews with stroustrup and in my opinion
quite insightful.

Ben
 
F

Frederick Gotham

Tony:
Perhaps the perceived inefficiency of doing the Program::Run() thing is
why it's not a low level C++ construct.


Actually, I was mocking what I deem to be a stupid attitude.

"main" is a perfect candidate for a function. The only reason anyone would
want to make some sort of class out of it is so they can, "hmm... look,
everything's object orientated, I can do Java me."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top