Thread: Basic Pointer Help

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    Basic Pointer Help

    Hi all,

    I'm having a problem in some code and I think it's because I've misunderstood how pointers are supposed to work. I've read the tutorials but perhaps I haven't understood them correctly. My aim is to post some code, explain what I think is supposed to happen and then maybe one of you lovely people could explain why it does not?

    Code:
    class MyClass
    {
         public:
                  int count;
                  int GetCount();
                  void AddOne();
    }
    
    MyClass foo;
    
    //this should point bar to the rvalue of foo?
    MyClass *bar = &foo;
    
    //this vector now holds the object foo
    vector.push_back(foo);
    
    //this should add 1 to count inside foo because above I declared bar as a pointer to foo.  The operations that I perform on bar should be done on foo as I told bar to point to the addressof foo
    
    bar->AddOne();
    
    
    for(int i = 0; i < vector.size(); i++)
    {
       //I want this to call foo.GetCount which was updated by bar.AddOne()
       vector[i].GetCount();
    }
    Just to say that this is pseudo code to try and demonosrate the point. You can presume that AddOne actually adds one to count and vector is a correct lvalue and the like.

    Does foo being inside a data structure matter to bar->AddOne()? More likely, have I got the wrong idea here?

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should post something that will compile, not random 1-line snippets of all your ideas.

    Each line you post is fine (in a context we assume), but that's not the same as the context in which you tried it (or understood it).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your theory is correct, but I don't believe it will work. I cannot say for sure since you have not provided the declaration for your vector, though.
    Remember that if you declare your vector as

    std::vector<MyClass> vector;

    Then, a copy of foo will be made and inserted into vector. Therefore, your action on bar (or foo) won't affect the object inside the vector.
    You would need a pointer to be stored inside the vector for it to work properly.
    (See smart pointers if you need to use pointers.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very basic pointer questions--pls help
    By tkm in forum C Programming
    Replies: 7
    Last Post: 02-04-2011, 02:54 PM
  2. Basic pointer q
    By Jasper in forum C Programming
    Replies: 1
    Last Post: 11-28-2009, 02:44 AM
  3. basic of array and pointer
    By jeanette in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 10:17 AM
  4. Basic pointer question
    By MacNilly in forum C Programming
    Replies: 5
    Last Post: 06-19-2007, 08:31 AM
  5. basic pointer question need help
    By venckman in forum C Programming
    Replies: 4
    Last Post: 04-23-2003, 11:25 AM