Thread: uncomfortable syntax in Vector

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    uncomfortable syntax in Vector

    hi i want to understand the syntax below and its acessing methods. can anybody tell me in a simple way.

    Code:
    class xxx
    {
     // data
    
    
    // function
    
    }
    
    
    vector<xxx *>  *abc  // what is the meaning of this. how to acess data with it
    
    
    and also
    
    vector<xxx *> & abc // what is the meaning of this ?
    
    
    although i have ideas on reference, pointers etc, still i  am feeling uncomfortable with this syntax. can anybody explain what are the meaning of these syntax?
    
    thanks
    blue_gene

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >vector<xxx *> *abc
    This is a pointer to a vector of pointers to xxx. Here is an example of how to set it up and then use it:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class xxx {
    public:
      void foo() { cout<<"Foofoo!\n"; }
    };
    
    int main()
    {
      xxx a;
      vector<xxx*> *abc = new vector<xxx*>;
    
      abc->push_back ( &a );
    
      (*abc)[0]->foo();
    }
    >vector<xxx *> & abc
    This is a reference to a vector of pointers to xxx. The same principles apply from above except you must assign a real vector to abc when you declare it and you don't need to dereference it:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class xxx {
    public:
      void foo() { cout<<"Foofoo!\n"; }
    };
    
    int main()
    {
      xxx a;
      vector<xxx*> base;
      vector<xxx*>& abc = base;
    
      abc.push_back ( &a );
    
      abc[0]->foo();
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    hi prelude, i have more questions on your code. ok, i am wrting steps where i am getting trouble.
    This is a pointer to a vector of pointers to xxx
    is it a 2D matrix ? bcoz int **A =>means A is 2D matrix. A is basically pointer to pointer to int. so from the similarity i have to conclude that the above is a 2D matrix.


    vector<xxx*> *abc = new vector<xxx*>;
    abc is pointing to what ? is it pointing to the vector or pointing to the class directly inside the vector?


    there is rule how to acess the memebers from the class if the class is like below.....

    class anyclass
    {

    private:

    int data;

    public:

    func() { cout<<"hi<<endl; }

    } *pointer;


    OR if the class is

    class anyclass
    {

    private:

    int data;

    public:

    func() { cout<<"hi<<endl; }

    } ;

    anyclass *pointer.

    anyclass object. // create an object


    // if i want to acess a method from the class then there is two way basically.

    1. pointer->func()


    2. object.func()


    you are writing
    abc->push_back ( &a );
    i think you are following option 1 assuming abc pointing to vector and inside vector xxx lies. am i right?


    (*abc)[0]->foo();
    this syntax is very much cryptic. not getting.

    push_back ( &a );[/
    ---why you are sending references or alias to the push back func.


    sorry i have generated many silly questions. i have more questions for the second part, but let me clear it up the first part first of all.

    thanks
    blue_gene

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is it a 2D matrix ?
    No, it's a pointer to an object that represents a 1D vector of pointers to objects. You can think of it like this:
    Code:
    // Warning: Bad ASCII art follows
    
    abc
     |  -------------------------------------
     |->|   |   |   |   |   |   |   |   |   |
        --|---|---|---|---|---|---|---|---|--
          |   |   |   |   |   |   |   |   |-> object
          |   |   |   |   |   |   |   |-----> object
          |   |   |   |   |   |   |---------> object
          |   |   |   |   |   |-------------> object
          |   |   |   |   |-----------------> object
          |   |   |   |---------------------> object
          |   |   |-------------------------> object
          |   |-----------------------------> object
          |---------------------------------> object
    >abc is pointing to what ?
    abc is pointing to a vector object.

    >i think you are following option 1 assuming abc pointing to vector and inside vector xxx lies.
    Yes, that sounds about right.

    >>(*abc)[0]->foo();
    >this syntax is very much cryptic. not getting
    abc is a pointer, so you have to dereference it to get to the vector that it points to. Once you have the vector (by dereferencing the pointer) you can use the subscript operator to get to the items contained by the vector. Then once you have one of the times, you can call member function by using the arrow operator because the items contained by the vector are pointers to objects.

    >>push_back ( &a );
    >why you are sending references or alias to the push back func
    This code is taking the address of a and passing it to the push_back function. In this context, & means the address-of operator that makes a pointer out of a variable.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    hi prelude, your example , diagram and explanations are very much helpful to me. you have explained nicely.

    i want to ask one small question about the phrase you used.

    your comment.
    No, it's a pointer to an object that represents a 1D vector of pointers to objects


    i would strongly suggest this phrase should be like below.....

    " No, it's a pointer to a class(//here vector class) that represents a 1D vector of pointers to class ."


    My reason
    __________

    i have changed objects to class only . bcoz if i write int * x , then
    we call x is a pointer to int(int is a data type and class is also a data type (user defined) . so i should write class instead of object)


    and also, we write pointer to class , not pointer to objects

    like the code below,



    class anyclass
    {

    private:

    int data;

    public:

    func() { cout<<"hi<<endl; }

    } *ptr; // ptr is a pointer to class NOT the object




    thats why i have suggested the phrase to be like that.


    anyway, may be it has no importance at all.

    thaks for the good explanation.

    P.S : is there anything pointer to objects ? my above code is not a pointer to objects syntactically
    blue_gene

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    i have changed objects to class only . bcoz if i write int * x , then
    we call x is a pointer to int(int is a data type and class is also a data type (user defined) . so i should write class instead of object)


    and also, we write pointer to class , not pointer to objects
    You seem confused. A class is a definition of a type. You can only have pointers to objects of that definition-- not to the definition itself. You could say: "I have a pointer to an object of class X." But stating that you have a pointer to a class is awkward and incorrect.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i would strongly suggest this phrase should be like below.....
    >" No, it's a pointer to a class(//here vector class) that represents a 1D vector of pointers to class ."
    A pointer doesn't point to a class because a class is simply a template (not to be confused with C++ templates) for an object. To be strictly correct, I would have said "an object of the vector class" and "to objects of the xxx class", but I felt that those distinctions could be omitted because they were so obvious. If we were speaking more informally and I felt you recognized the distinctions, I would merely have said "pointer to vector" and "pointers to xxx" or even "vector pointer" and "xxx pointers".
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM