Thread: pointer's ->

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    pointer's ->

    Hi, all~

    I read an article which said we could follow the format like this:

    Code:
    //assume we have a class named myClass;
    class myClass
    {
      public:
        int value=0;
    };
    //and then our pointer comes;
    myClass *p;
    pointer->value=10;
    as known a pointer is variable stored address, how could it have a property here ???

    thanx for any explanation~
    Never end on learning~

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Thats a segfault waiting to happen. you have a pointer but it points into random memory and not at an object.

    this is more correct....
    Code:
    myClass *p=new myClass;
    p->value=10; // ok now.
    delete p;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Stoned_Coder
    Thats a segfault waiting to happen. you have a pointer but it points into random memory and not at an object.

    this is more correct....
    Code:
    myClass *p=new myClass;
    p->value=10; // ok now.
    delete p;
    sorry but I couldnt figure out what it should work for since not object at all...
    Never end on learning~

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    and I have no idea whether the dynamic allocation code always exists when we declare a pointer to class, yes or no ?
    Never end on learning~

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    myClass realObject;
    myClass *p;
    //p->value=10; //<--not yet! doesn't point to anything...
    p = &realObject;
    p->value=10; //...fine...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Sebastiani
    myClass realObject;
    myClass *p;
    //p->value=10; //<--not yet! doesn't point to anything...
    p = &realObject;
    p->value=10; //...fine...
    woh~ I got it ! dynamic allocation just assign some memory to our cute pointer when running, thanx !

    and... what about p.value ? it works fine but I couldnt imagine how a variable could have properties... I'm stuck here

    any ideas ?
    Never end on learning~

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Salem
    > what about p.value
    No, following Sebastiani's example

    realObject.value = 10;
    p->value = 10;

    Do the same thing to the same object, but by different routes.
    sorry, I just mean p->value here. I am confused with a variable with contains properties, it's dizzy...
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quadtree Node -> pointers to the children node
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 06:38 PM
  2. Q: Pointers to Classes '->' notation
    By Howie17 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2008, 10:09 AM
  3. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM