Thread: question about pointers

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    17

    question about pointers

    when declaring:
    Code:
    Account nAcnt1;
    and

    Code:
    Account *nAcnt2;
    the latter is a pointer to Account class.

    but if we go:

    Code:
    nAcnt2=new Account;
    what do u call this new type?

  2. #2
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    We call it
    "A pointer pointing to a dynamicaly allocated object of an 'Account' class."
    Yes I'm trying to confuse you.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by hoangvo
    when declaring:
    Code:
    Account nAcnt1;
    and

    Code:
    Account *nAcnt2;
    the latter is a pointer to Account class.
    Nope, the latter is a pointer to an Account object. None exists at the (uninitialized) location it's pointing at.

    Quote Originally Posted by hoangvo
    but if we go:

    Code:
    nAcnt2=new Account;
    what do u call this new type?
    nAcnt2 is still a pointer to an Account object. Its type won't change. This time an Account object exists at the location it's pointing at.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Pointers are variables that store addresses. With this statement:

    Account *nAcnt2;

    you create a pointer variable called nAcnt2, which you have declared will store the address of an Account object. I prefer to use this format:

    Account* nAcnt2;

    That format makes it clear the variable name is 'nAcnt2', and the type is Account*.

    The 'new' keyword dynamically creates an object and returns its address in memory. So, here:

    nAcnt2=new Account;

    you create an Account object, and assign its address to the variable nAcnt2.
    Last edited by 7stud; 07-24-2005 at 12:19 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    Account a;
    Account *p = &a;
    Declares a pointer to a. You can access a as a or *p.

    Code:
    Account *p = new Account;
    Declares a pointer to a newly allocated Account. But in this case, the only way you can access this Account is through *p.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM