Jump to content

Mine less asked questions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
Inspired by the another thread I've a few questions for you.
Question 1: How do you clear(delete all elements), or erase only a part of a stringstream?
If you get a strinstream objetc like:
stringstream abc;

abc<<"Hai";

cout<<abc.str()<<'\n';

abc<<23;

cout<<abc.str()<<'\n';
Will output 'hai' and after 'hai23'.How do you erase only 23 or clear both elements?

Question 2: How OO concepts like inheritance and polymorphism are handled in assembly?
I don't know too much asm, and I think it is only an abstraction of the compiler, but what OO results in assembly code?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Question1 you can reset the stringstream using str():
  stringstream abc;
  abc<<"Hai";
  cout<<abc.str()<<'\n';
  abc.str("");
  abc<<23;
  cout<<abc.str()<<'\n';
  return 0;
This will output "Hai" on one line, "23" on the next. There is also a seekp() function that you can use to move your input to various locations. Not sure if that fully answered your question.

Question2A C++ compiler typically creates function maps for each method. The map points to the correct version of the method based on the particular class instantiated. You can actually do OOP style programming in C by using the same concepts. C++ compilers also generally do what is referred to as "name mangling" on overloaded functions to avoid ambiguous function names. The result is that the resulting Assembly is NOT OOP! The OOP concepts are reinterpretted into non-OOP code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
OOP doesn't really do anything fancy, if you think about it. It just abstracts stuff away from the programmer into the compiler. For example, member functions of classes take an extra hidden parameter that points to the object that the function is being called on. So classes are basically structs with functions associated with them. Once you get that concept, you can see how easy it is to do OOP in assembly language--it's not much different from ordinary C programming.
sudo rm -rf /