Class Usage simple question
Hello,
Once again i have simple understanding failure about classes.
Lets say i have class called MyClass, with in I have my constructor/ distracter, some functions.
Now i want to "associate" identificator with my class name so I can use it, right?
Well - here I have 2 options:
MyClass a;
...
a.whatever();
or:
MyClass *a = new MyClass ();
....
a->whatever();
....
delete a;
My question is : Is the only different between the two is that in first case i cannot pass arguments to my constructor and in 2nd i can? since i noticed the destructor never called in 1th definition case.
What's better to use?
Thanks in advance...
[730 byte] By [
portnov] at [2007-11-20 1:29:11]

# 1 Re: Class Usage simple question
You can pass arguments in both the cases.
MyClass a(arguments);
...
a.whatever();
# 2 Re: Class Usage simple question
class foo
{
public:
foo(int an_arg, int another);
// ...
};
void f()
{
foo a_f(5, 5); // destructor gets called on exit from f()
foo* p = new foo(5, 5);
delete p; // destructor gets called here
}
Does that help?
# 3 Re: Class Usage simple question
You can pass arguments in both the cases.
MyClass a(arguments);
...
a.whatever();
then whats the difference? only the distracter call?
When should i use each method?
# 4 Re: Class Usage simple question
Ah, Krishnaa - you got in before me...
# 5 Re: Class Usage simple question
then whats the difference? only the distracter call?
When should i use each method?The class related stuff is all the same. The difference is where the object is created... stack or the free store?
Stack access is faster while creating objects on free store is a small overhead compared to it.. however stacks usually have a limitation of being just a couple of MBs.. (but thats usually enough too..) while free store is huge. Also, as you saw with new/new[], you need to take care of calling delete/delete[] unless you use smart pointers.
When you deal with huge storage requirements - you use free store else stack is fine.. But for that matter - you will use some container - they will internally do the memory management for you (on heap).
So, as far as you are concerned - you should prefer stack allocation - if that's a problem use the free store (using new or some container that maintains dynamic storage).
# 6 Re: Class Usage simple question
then whats the difference? only the distracter call?
Even the destructor is called in both cases, I hope thats what was your point.
Ah, Krishnaa - you got in before me...
just happened to be there, didn't want to come in your way. :D
# 7 Re: Class Usage simple question
Well - let me give an example:
#include <stdio.h>
class MyClass{
public:
MyClass();
~MyClass();
void begin();
};
MyClass::MyClass() {
printf("CONST\n");
}
MyClass::~MyClass() {
printf("DIST\n");
}
void MyClass::run() {
printf("RUN...\n");
}
int main(int argc, char* argv[])
{
MyClass*a = new MyClass();
a->run();
getchar();
delete a;
return 0;
}
Output here will be :
CONST
RUN
DIST
Now - running the same piece using:
MyClass a;
a.run();
Will resolve in output:
CONST
RUN
Ideas why?
PS: Thanks for all the quick replays so far - covered almost everything i wanted to know about the issue, stick just a little bit more with me here :)
# 8 Re: Class Usage simple question
Ideas why?
The destructor is called when the object leaves scope, which is when main is exiting. At this point, my guess is that the runtime environment is being deconstructed and all output is ignored.
Rest assured that the destructor is being called.
Interesting, though.
Jeff
# 9 Re: Class Usage simple question
Now - running the same piece using:
MyClass a;
a.run();
Will resolve in output:
CONST
RUN
Ideas why?
How do you run it? If you, for example, debug it - the console window will close before you'll notice that output.
Try "running" it (with Ctrl+F5) - the Studio will add a "Press any key..." prompt, and that will keep console window open for you to make sure the "DIST" is there.
Or just run your app from the command prompt.
# 10 Re: Class Usage simple question
Or perhaps the output needs to be "flushed" after the write.
I don't use printf. Is there a global flush() call? MSDN has a _flushall, but I don't know how standard it is.
Jeff
# 11 Re: Class Usage simple question
How do you run it? If you, for example, debug it - the console window will close before you'll notice that output.
Try "running" it (with Ctrl+F5) - the Studio will add a "Press any key..." prompt, and that will keep console window open for you to make sure the "DIST" is there.
Or just run your app from the command prompt.
Uh...
Maybe the stupidest error i made in a while - i used system("PAUSE"); just before return 0; for looking at the output, the thing is: the "DESTR" outputs somehow AFTER the pause... at delete a: usage "DESTR" outputs before pause...
MyClass a;
a.run();
system("PAUSE");
return 0;
}
Looks like "try and error" method failed me here - Thanks you all for quick and informative replays - appreciated.
# 12 Re: Class Usage simple question
If you really want to be convinced, try it this way:
void func()
{
printf("func in...");
MyClass a;
a.run();
printf("...func out");
}
int main()
{
func();
printf("after func");
system("pause");
}
You should see that the destructor is called when func exits (i.e. you'll see its output between "func out.." and "after func").
# 13 Re: Class Usage simple question
No,no - it was just silly misunderstanding of the mechanics behind, judging by one simple misleading example.
Thanks you again all for your help and support.