typedef help

This is a discussion on typedef help within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm a little rusty with C++. Well, actually this is something that in the past I kind of overlooked ...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    18

    Question typedef help

    Hi, i'm a little rusty with C++. Well, actually this is something that in the past I kind of overlooked and never needed. And after around six months of doing absolutly no programming what-so-ever; it seems I need to learn a bit.
    I would really appreciate it if someone could please explain what's happening in the following code (it's the typedef bit i'm concerned with):

    Code:
    struct BBRECORD
    {
    	/*
    	stuff
    	*/
    };
    
    typedef std::vector<BBRECORD*> BBRECORD_LIST;
    After this, the idea seems to be to create a class which is able to add BBRECORDS to a BBRECORD_LIST but i fail to see how this can be done because I don't fully understand what's going on in that code.

    Thanks for any relpies.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    The typedef doesn't create any new class. It just causes BBRECORD_LIST to be exactly synonymous with std::vector<BBRECORD*>.

    So anywhere you would use the type std::vector<BBRECORD*> you could also use BBRECORD_LIST. Proceed as normal...

  3. #3
    Registered User
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    18
    Quote Originally Posted by brewbuck View Post
    The typedef doesn't create any new class. It just causes BBRECORD_LIST to be exactly synonymous with std::vector<BBRECORD*>.

    So anywhere you would use the type std::vector<BBRECORD*> you could also use BBRECORD_LIST. Proceed as normal...
    Thanks for the fast reply.

    I think you misunderstood my question though. It's my aim to create a class later. I understand that typedef does not create a class. I suppose my question is; what exactly is std::vector<BBRECORD*>; it's a vector, but what relevance does the BBRECORD* have?

    I suppose I worded my initial post incorrectly, my question is not so much about the typedef. Apologies.

    Thanks again.
    Last edited by switchcase; 10-04-2007 at 04:54 PM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    It's a pointer. Google it.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by switchcase View Post
    Thanks for the fast reply.

    I think you misunderstood my question though. It's my aim to create a class later. I understand that typedef does not create a class. I suppose my question is; what exactly is std::vector<BBRECORD*>; it's a vector, but what relevance does the BBRECORD* have?

    I suppose I worded my initial post incorrectly, my question is not so much about the typedef. Apologies.

    Thanks again.
    a std::vector<BBRECORD*> is a vector of BBRECORD*. A BBRECORD* is a pointer to a BBRECORD. So it's a vector of pointers to BBRECORD objects -- not to be confused with a vector of BBRECORD objects.

  6. #6
    Registered User
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    18
    Quote Originally Posted by Desolation View Post
    It's a pointer. Google it.
    I know that.


    a std::vector<BBRECORD*> is a vector of BBRECORD*. A BBRECORD* is a pointer to a BBRECORD. So it's a vector of pointers to BBRECORD objects -- not to be confused with a vector of BBRECORD objects.
    I thought so. But how does this make it possible to dynamically create BBRECORDs?

    Thanks.
    Last edited by switchcase; 10-04-2007 at 05:26 PM.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    BBRECORD_LIST records;
    records.push_back(new BBRECORD);

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And it's your responsibility to delete that object later.

    (The little pedantic demons in me are crying, "Not exception-safe!" but I don't want to load that on the OP right now.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Yeah I was gonna say at least use auto_ptr or something, if you *really* want a vector of pointers to objects. I'm positive a vector doesn't try to store very much on the stack.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You're not allowed to use auto_ptr as a container element. You've got to use shared_ptr or something like it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    Huh ?! Why did someone delete my post ? I wrote a post to say that he had to use boost::shared_ptr and it's gone now =/

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I think it failed to submit or something. I would see it if it was deleted.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Desolation View Post
    Huh ?! Why did someone delete my post ? I wrote a post to say that he had to use boost::shared_ptr and it's gone now =/
    Screenshot or it never happened! lol...

  14. #14
    Registered User
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    18
    Thanks for the replies everyone.

    You have been very helpfull. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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