Instances of classes

This is a discussion on Instances of classes within the C++ Programming forums, part of the General Programming Boards category; Here's another C++ question that I'm curious about. In Java you only use the new keyword to create an instance ...

  1. #1
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175

    Instances of classes

    Here's another C++ question that I'm curious about. In Java you only use the new keyword to create an instance of a class but in C++ there are a few ways. You can use

    1) MyClass *c = new MyClass();

    which returns a pointer to an instance of MyClass and there is

    2) MyClass c;

    which puts the actual instance of the class in the variable c and then there is

    3) MyClass *c;

    Both 1 and 3 are pointers to the class... so what's the difference. I know that since the first was created with new you have to later delete it. Why do you need to do this?

    When do I use which?
    Last edited by JasonLikesJava; 05-31-2002 at 11:48 AM.
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    63
    It does really depend on what you want to do with the instance. If you only want to acces the memory location where an instance of the class is, then obviously you use a pointer.

    The second one, is just like declaring a 'int x' for example.

    Hope this helps.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  3. #3
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Can't you do the same thing with all of them?
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    1 is an object created on the heap (which is pointed to by the pointer c), where it'll remain until it's explicitly deleted (unless you're using some form of smart pointer or garbage collector addon).

    2 is an object created on the stack which will remain until the scope in which it was created ends.

    3 is not an object, and cannot be used as one (unless it's pointed at one). It's a pointer to an object, and can be pointed at something created by either of the first two methods. However, the scoping rules still apply to what it is pointed at.

  5. #5
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Hmmm... it looks like I should be using the second one more often instead of 1. Coming from the Java community I naturally want to use the new keyword.

    Thanks for the clarification
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes storing instances of own class.
    By HIM in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2006, 09:18 AM
  2. "Selecting" Instances of Classes
    By SnS CEO in forum C++ Programming
    Replies: 12
    Last Post: 08-04-2005, 08:50 PM
  3. Classes and Destroyed Instances
    By FWGaming in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2005, 11:02 PM
  4. Can not reach the classes and create instances of them
    By kromozom in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2005, 08:31 AM
  5. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21