pointer vs non-pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carmen Sei

    #1

    pointer vs non-pointer

    to delcare an object with pointer -
    MyCar * mycar = new MyCar;
    mycar->Create();

    =============== =========
    to delcare an object without pointer -
    MyCar mycar = new MyCar;
    mycar.Create();

    =============== =========
    for pointer we use ->
    for non-pointer we use . (dot)

    =============== =========
    I think for Java equivalent the -is same as Java's . (dot)

    =============== =========
    should I always you the MyCar * mycar = new MyCar; to initalize a new
    Object?

    since using -is same as using (dot) in Java.

    =============== =========
    when are the time to use the (dot) but not -(non-pointer) then?
  • Fred Zwarts

    #2
    Re: pointer vs non-pointer

    "Carmen Sei" <fatwallet951@y ahoo.comwrote in message news:qn0ft3tpem bv7h50octvok1o1 5nmtqkcbb@4ax.c om...
    to delcare an object with pointer -
    MyCar * mycar = new MyCar;
    mycar->Create();
    Why is this? Does the constructor of MyCar fail to create mycar?

    =============== =========
    to delcare an object without pointer -
    MyCar mycar = new MyCar;
    Did you try to compile this? It won't work.
    The types are different at both sides of the =.
    Left the type is MyCar (object), right the type is MyCar* (pointer to object).
    Maybe you meant:

    MyCar mycar;
    mycar.Create();

    =============== =========
    for pointer we use ->
    for non-pointer we use . (dot)

    =============== =========
    I think for Java equivalent the -is same as Java's . (dot)
    In Java (almost) everything is a pointer, so only one operator is needed.
    In C++ there are objects and pointers to objects,
    so two operators are used for two different cases.
    "mycar->" is equivalent to "(*mycar)." .

    =============== =========
    should I always you the MyCar * mycar = new MyCar; to initalize a new
    Object?
    No, only for dynamically created objects. For other objects use MyCar mycar;.

    since using -is same as using (dot) in Java.
    Approximately.
    And Java has no equivalent for the C++ . (dot) operator.

    =============== =========
    when are the time to use the (dot) but not -(non-pointer) then?
    When you have an variable that is not a pointer,
    which may happen in fact more often than having a pointer.

    Note further that, in contrast to Java, C++ has no automatic garbage cleaner
    for dynamically created objects. Objects created with "new" need an explicit
    "delete" to free their resources, otherwise a memory leak will be the result.
    This is another reason to avoid the use of new, where possible.

    Comment

    Working...