Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public String toString()
{

    // Save the current position of the list
    int oldPos = currPos();
    int length = length();
    StringBuffer out = new StringBuffer((length() + 1) * 4);

    moveToStart();
    out.append("< ");
    for (int i = 0; i < oldPos; i++) {
      out.append(getValue());
      out.append(" ");
      next();
    }
    out.append("| ");
    for (int i = oldPos; i < length; i++) {
      out.append(getValue());
      out.append(" ");
      next();
    }
    out.append(">");
    moveToPos(oldPos); // Reset the fence to its original position
    return out.toString();
  }


What I have tried:

i tried anything but still cant get the solution
Posted
Updated 10-Oct-21 20:04pm
v2

1 solution

C++ and C# / Java are not the same language, though they share many similarities.
If C# and Java you specify the access modifier with the function definition:
C#
public void foo() { ...}
public void bar() { ...}
private void foobar() {...}
In C++ it's different, the access modifier affects a block of code:
C++
public:
void foo() { ...}
void bar() { ...}
private:
void foobar() {...}


And that's a pretty bad practice you have going on there: a ToString implementation shouldn't affect anything outside the function - known as a side effect - as it's not at all obvious that it will happen, and ToString can be called implicitly as well as explicitly so you might not be aware that the side effect is happening at all.
So wherever you got that code from, it's probably not a good example at all ...
 
Share this answer
 
Comments
CPallini 11-Oct-21 2:42am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900