newbie reading code

This is a discussion on newbie reading code within the C++ Programming forums, part of the General Programming Boards category; I'm reading a source code and really need some help... Thanks! What does the following 2 lines mean? Why it ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    newbie reading code

    I'm reading a source code and really need some help... Thanks!
    What does the following 2 lines mean?

    Why it is no int items? What is the significant of set?
    Code:
    set<int> items;
    Below is a constructor for a class Transaction. What does : length(l) represents?
    Code:
    Transaction(int l) : length(l) {t = new int[l];}

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    can't help you with the first one, somebody else will.

    on the second one: basically, it initializes length to l. notice it's an initialization, not any old assignment.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Thanks for your help!!

    sorry, i'm too lousy, n the textbook i'm reading doesn't tell me what it is. I got another question regarding another constructor of the class Transaction.

    Does the code below means that the object Transaction is taking in a pointer, which is pointing to another object Transaction?

    Code:
     Transaction(const Transaction &tr);

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by franziss
    Below is a constructor for a class Transaction. What does : length(l) represents?
    Code:
    Transaction(int l) : length(l) {t = new int[l];}
    That above would be the same as putting:

    Code:
    Transaction(int l) {
    t = new int[l];
    length = l;
    }
    Its obvious that on constructing a class that you will want some initial value for the variables in that class, so you can use : as it does, then one of the classes values, then the value you want in brackets, in this case its a value passed in through the parameter. Another case you might set say xPosition(0) and yPosition(0). It saves space, its more clear, and I forget but it may be quicker (not sure).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    http://www.cppreference.com/cppset/

    this is a set and what you can do with a set.

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by franziss
    Does the code below means that the object Transaction is taking in a pointer, which is pointing to another object Transaction?

    Code:
     Transaction(const Transaction &tr);
    Sort of.

    It means the constructor in the class Transaction has the parameter which points to an object of Transaction. Its called a copy constructor, and you basicly can make an object of Transaction, and in the parameter pass another object of Transaction you've made, and it will copy all the values of that object to the new object.

    I believe this would be how its done, but I've never used it.. it'll be correct if its wrong, I'm sure
    Code:
    int main ()
    {
      Transaction trans1;
    
      trans1.value = 5;
      trans1.name = "bob";
    
      Transaction trans2(trans1);
    
      cout << trans2.value << endl; //5
      cout << trans2.name << endl; //bob
    
    }
    The copy constructor is only there by default, if you make your own constructor it wont be there unless you remake it yourself.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> The copy constructor is only there by default, if you make your own constructor it wont be there unless you remake it yourself.

    This part is incorrect. The copy constructor is there by default, even if you make your own constructor (it is the default constructor with no parameters that goes away if you define your own constructor).

    set is the standard set container, useful for holding items (in that case ints) that can be looked up quickly and kept in sorted order. A set uses templates to identify the type of object it holds, which is why the <int> means that it holds int values.

    The initializer list is what comes after the : in the constructor and before the body of the constructor. It is good practice to always use the initializer list, even for POD types like int that gain no performance improvement. It is good practice because of code clarity and consistency with other objects that must be initialized in the intializer list or that gain performance benefits by doing so.

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    This part is incorrect. The copy constructor is there by default, even if you make your own constructor (it is the default constructor with no parameters that goes away if you define your own constructor).
    Really? This link says both do.

    http://www.cplusplus.com/doc/tutorial/tut4-1.html

    It is important to realize that both default constructors: the empty construction and the copy constructor exist only if no other constructor is explicitly declared. In case that any constructor with any number of parameters is declared, none of these two default constructors will exist. So if you want them to be there, you must define your own ones.
    It's late, but I don't think I'm reading it wrong, unless they have it wrong.. I'd check a book, but I still havent ordered one.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Yep, that is very clear. It is also wrong. Simple example - copying should cause a compiler error if it doesn't exist:
    Code:
    #include <iostream>
    
    class Test
    {
    public:
        Test(int i) : data(i) { }
        int data;
    };
    
    int main()
    {
        Test t1(15);
        Test t2(t1);
        std::cout << t1.data << " " << t2.data << std::endl;
    }

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Thank for your help guys! I know what is going on now.... =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help reading code
    By cjohnman in forum C Programming
    Replies: 3
    Last Post: 04-08-2008, 09:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 03:04 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  5. Newbie Source Code question. Hlp Plz?
    By BOOGIEMAN in forum Game Programming
    Replies: 4
    Last Post: 12-14-2001, 03:30 AM

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