Thread: I have a C++ programming test to do and I don't understand what thay wan't me to do

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Question I have a C++ programming test to do and I don't understand what thay wan't me to do

    Her is the information, Is it somebody who have an ide, please give tips, i don't understand it.

    Implement a reference counting string class in C++ with at least support for
    assignment and concatenation.
    Write a test case which goes through all methods and alphabetically sort
    some strings of this kind using a STL container.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Reference counting - Wikipedia, the free encyclopedia

    Reference counted strings are such that share memory. Reference counter is also shared and is an integer that is incremented when memory is acquired and decremented when released.

    In short:
    Code:
    shared_string aa = "This is shared string"; // memory is allocated for text, reference counter is set to 1
    shared_string bb = aa; // bb points to the same memory location as aa, reference counter is incremented and is 2 now
    When strign is destroyed (or changed) the reference counter is decremented and if it reaches 0, memory is freed.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Question

    Thanks kmdv!

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Question

    I understand Reference counted strings and Reference counter and that they share memory.
    But what should the reference counting string class in C++ with at least support for assignment and concatenation do!

    How should the test case which goes through all methods connects with the reference counting string class?

    Alphabetically sort some strings of this kind using a STL container in the test case, I think is another problem not connected to the reference counting string class, do you think that is correct?

    Thanks for help!

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    concatenation is like using += in a way it combines two strings...
    so if you have 3 strings you could do something like
    cc = aa;
    cc += bb;

    aa would have a count of 2 and bb would have a count of two. At least that is my understanding.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Jan-Erik View Post
    Alphabetically sort some strings of this kind using a STL container in the test case, I think is another problem not connected to the reference counting string class, do you think that is correct?
    No, it is clearly all meant to use the same class. Your class should simply implement the less-than operator, then I would suggest either throwing several into a vector and using std::sort, or just inserting them all into a std::set.

    There is nothing that is not clear about what is required for this, to someone who knows what they're doing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by ~Kyo~ View Post
    concatenation is like using += in a way it combines two strings...
    so if you have 3 strings you could do something like
    cc = aa;
    cc += bb;

    aa would have a count of 2 and bb would have a count of two. At least that is my understanding.
    This would require additional information and string data would not be contiguous in memory.
    += operator should instantiate a new object with its own reference counter (or use an existing if the resulting string already exists in the container).
    aa, bb, and cc would have a count of 1 (assuming aa and bb are not empty).

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    depends on the implementation of += as you can overload it, but yes with the current implementation, no that code won't do what he wants. Then again I thought it was a test like schoolwork test. I just wanted to give a vague answer that would give an idea of how I read the question.

Popular pages Recent additions subscribe to a feed