C or C++?

puissante puissante at lrc.puissante.com
Mon May 30 12:15:01 EDT 2005


Steven W. Orr wrote:

> I am not a c++ wiz, but I always thought that that was a fundamental 
> problem. How do you create a class that doesn't always call new if you 
> know that the lifetime of the storage would be appropriate for the stack 
> in certain cases?

You can allocate a class on the stack thusly:

class MyClass
{
...
};

main(...)
{
	MyClass mc;
	...
}

In general I would not advocate allocating classes on the stack, because 
some classes could be rather large in their size requirements. There are 
also some subtle "stack-effect" issues to catch the unwary...

C++ has become quite sophisticated over the years, especially with the 
use of templates and the like. My big criticism of C++ is that it has 
become *too* complicated, and much of the added complexity is really a 
cover for the deficiencies inherent in the language itself.

As much as I like C++ and have used it in the past, I must state that if 
you find yourself relying more and more on the "extended features", it 
may be you should be using another language more suited for the task. 
Unless you are doing serious number crunching or heavy-duty 
event-handling, even "scripting" languages like Python may be a better 
choice, as it is much more object-oriented than C++, has first-class 
functions that C++ does not have (in the core language anyway), and has 
many other niceties to boot. Allocation and pointer issues are simply 
non-existent, leaving you to more focus on solving the problem rather 
than on low-level annoyances. Yeah, I know it's "macho" being able to do 
C and C++ pointer arithmetic, but still! ;-)

Again, it's all a matter of what your needs are. As having mastered so 
many programming languages in the past that I've lost count, I tend to 
be much more "solutions-oriented" than "language-oriented".

-Fred



More information about the gnhlug-discuss mailing list