Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a class within the same project?

Status
Not open for further replies.

scecilia

Programmer
Jul 8, 2002
35
US
Hello,

I created a dialog based project. Then, via the insert, I inserted a C++ class: ExecCmd(). In the main C++ class, how can I call/declare an object to ExecCmd()?

I am trying: ExecCmd x = new ExecCmd();

But I get an error C2440: 'initializing' : cannot convert from 'class ExecCmd *' to 'class ExecCmd'

I do not get it.

Thanks,
 
I had tried that and it works, but why do I need to declare it as a pointer?

Thanks.
 
You don't, you can also do just ExecCmd x;

You can NOT new it though, this is only done when using a pointer. Basically there's not much difference between them, but there are things you must not forget and you might want to consider:

If you declare ExecCmd x; like this in a function, it will be allocated on the stack and therefore automatically freed when it goes out of scope. This might come in handy, since this you can't cause accidental memory leaks.

On the other hand, when it's a large object, you might want to consider declaring it as a pointer and new it, which allocates it on the heap instead of the stack. However, now only the pointer will be automatically deleted when it goes out of scope NOT the object itself. If you forget to delete the object before the pointer goes out of scope, you will have caused a memory leak.
Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top