about pointers to a class/struct

This is a discussion on about pointers to a class/struct within the C++ Programming forums, part of the General Programming Boards category; hello, am confused..., sometimes i see pointers to a class as their declaration, ie, CLASS_A* pA; then, somwhere inside their ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    about pointers to a class/struct

    hello, am confused..., sometimes i see pointers to a class as their declaration, ie,

    CLASS_A* pA;

    then, somwhere inside their function they would do this,

    pA = new CLASS_A:

    that is perfectly understandable, because you can't use an uninitialized pointer anyways.., what's confusing about this is,
    why they didn't just decalred it as normal object, not a pointer to this class ie,

    CLASS_A a;

    then, they wouldn't need to initialize it anymore and they could easily access the member functions etc.,

    so why, actually a lot of people does that?

    thanks for your sides,

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    because 'CLASS_A a' will commonly allocate memory on the stack. If you need to allocate an object in heap space without using a pre-defined class then you'll need to use a pointer and the new keyword.

    You may need to use the 'heap' instead of the 'stack' because you don't know how much memory you require until runtime (you don't know the precise object or number of objects that you'll need), or you need the object to remain in memory after the function it was allocated in returns.

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    And, as I am finding out, if you do it like this:

    Class_A* pA = new Class_A;

    You can also do THIS:

    Class_A* pA = new Class_B;

    provided Class_B is derived of Class_A.

    Then if you call virtual functions in Class_A, and Class_B has a function with the same name, the Class_B function will be called. Called late binding polymorphism, or something like that.

    It is neat, I'm experimenting with it right now.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hi thanks for the reply guys, but i still got some queastions...,


    >or you need the object to remain in memory after the function it was allocated in returns.

    ahh., you mean, if i declare the object in one of a function, i could still use it huh? whereas a not-pointer declaration would destroy the object..,

    >If you need to allocate an object in heap space without using a pre-defined class then you'll need to use a pointer and the new keyword

    okey pre-defined class? what does that exatly mean?

    sorensen, what you've just told me, is that the most possible reason why would someone decalre a pointer to a class instead of just declaring it the orindary way?

    thanks guys!!

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >ahh., you mean, if i declare the object in one of a function, i could still use it huh? whereas a not-pointer declaration would destroy the object..,

    Yes, but you still have to maintain a pointer to this object. You could pass a pointer to a function, and get an allocated object in return without having to pass a potentially expensive object (an object larger than the size of a pointer) between functions.

    >okey pre-defined class? what does that exatly mean?

    A class that forms part of the C++ standard library, that depending on your needs will handle stuff for you.

    >sorensen, what you've just told me, is that the most possible reason why would someone decalre a pointer to a class instead of just declaring it the orindary way?

    Yep. Until someone corrects me. Imperito's explaniation is just a more in depth version of "you don't know the precise object".

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    oh i'm sorry, i forgot to mention something, this declarations are globals so...,

    >Yes, but you still have to maintain a pointer to this object. You could pass a pointer to a function, and get an allocated object in return without having to pass a potentially expensive object (an object larger than the size of a pointer) between functions.

    well, they are globals so i wouldn't care what would happen between function calls..., my point here is, i tend to see people declare pointer to a class wheras i can easily change it to normal declaration and it would still work..., if you could pls provide more reason behind this?

    thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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