Thread: is push_back() a overloaded function ?

  1. #16
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    i am so sorry. its working. may be that was a bad copy.i checked again. this time its working.

    did you notice push_back() is taking a pointer p . but when i usued pointer p in my original code push_back() gave me an error. thats why i wrote //illegal.

    what push_back() actually demands ? does it demand pointer and argument both . then i have to call it a overloaded function !!

    its really a peculiar behaviour of push_back().....

    i may be wrong somewhere.....
    blue_gene

  2. #17
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    push_back() takes any type of parameter which matches the vector declaration.
    Code:
    vector<TYPE> v;
    void push_back( const TYPE &val );
    if you create a vector of int then you push back an int, if it's a float then push_back float, if it's MyObject then push_back MyObject, if it's a container for pointers to MyObject then push_back a pointer to MyObject
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #18
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    it works here as well...although g++ is giving a warning with -Wall flag

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #19
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    >>if you create a vector of int then you push back an int, if it's a float then push_back float, if it's MyObject then push_back MyObject, if it's a container for pointers to MyObject then push_back a pointer to MyObject


    -----ok. i see ...its the matter then . thanks for the explanation.
    its ok. i understand about the usage of push_back() function. i understand how to handle it.




    if you dont mind can i ask one more confusion.

    see your definition ......
    vector<TYPE> v;
    void push_back( const TYPE &val );

    now replace TYPE by int*

    vector<int*> v;
    void push_back( const int* &val ); => void push_back(cont int val) // so push_back now demand simple argument !! bcoz * & == Doomed.

    sorry for the last silly and bad mathematical calculation.


    anyway.....i understand how to use push_back() now. your rule is ok.

    thanks
    blue_gene

  5. #20
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    a pointer reference is perfectly legal. I was once working with a linked list somebody made with a traverse function that took a function pointer as its parameter. The function pointer had to take in a Type &. So when I made a linked list of CObject * 's, I had to make a function like this to pass to Traverse:

    Code:
    bool Func(CObject * & obj)
    {
    }

  6. #21
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't confuse pointers with references - they are different.

    "const T &" is not a pointer, it's a constant reference to type T.

    The reference part (&) says - "don't make a copy and don't call any copy constructors"
    The "const" part says - "even though it's a reference, I promise not to modify it"

    For most native types, there is very little overhead in making copies.
    For class/struct types, the overhead can be large. So all STL containers (that I know of) will only make a single copy of an object when you add that object to the container.

    gg

  7. #22
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    >>"const T &" is not a pointer, it's a constant reference to type T.
    ....hmmm.


    thanks.....you guys are very helpful and cordial. i understand where i was wrong......i learned a lot. its ok.
    blue_gene

  8. #23
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by JaWiB
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
    vector<char* > z;
    char y[2];
    y[0]='a';
    y[1]='b';
    y[2]='\0';
    z.push_back(y);
    cout<<z[0]<<endl;
    cout<<z[1]<<endl;
    }
    That won't work as intended.
    The first cout will print 'ab', the second is undefined behaviour.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-04-2008, 05:57 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Problem with overloaded function templates
    By New++ in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2005, 04:00 PM
  5. Cannot resolve overloaded function
    By Mithoric in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2003, 03:40 AM