c++parsing simple text – %%experience

No insight. Just some personal experience as learning notes.

In my C programs, I relied exclusively on gets(). (The two getline() solutions require c++ stream objects.) Then I parse the c-string raw data with the c-str functions.

In my C++ programs, I used getline() exclusively. Which getline()? Probably I used http://www.cplusplus.com/reference/string/string/getline/. Once I have an entire line in a std::string, I parse it with the string methods.

There are alternative approaches that I only used lightly. I wrote test program in another post illustrating getline + stringstream.

2 simple singleton classes – c++

 

#include
#include
using namespace std;

template
class StaticFieldSingleton {
private:
  static StaticFieldSingleton instance_;
  StaticFieldSingleton() {
    cout << "StaticFieldSingleton() ctor\n";
  }
  StaticFieldSingleton(StaticFieldSingleton const &);
  StaticFieldSingleton& operator=(StaticFieldSingleton const &);
public:
  static StaticFieldSingleton& getInstance() {
    return instance_;
  }
};
template StaticFieldSingleton StaticFieldSingleton::instance_;

class SimpleSingleton { //based on [[effectiveC++]] P222
  SimpleSingleton() {
    cout << "SimpleSingleton()\n";
  }
  SimpleSingleton(SimpleSingleton const &);
  SimpleSingleton& operator=(SimpleSingleton const &);
public:
  static SimpleSingleton& get_instance() {
    static SimpleSingleton instance;
    return instance;
  }
};

int main() {
  StaticFieldSingleton::getInstance();

  SimpleSingleton& ins1 = SimpleSingleton::get_instance();
  SimpleSingleton& ins2 = SimpleSingleton::get_instance();
  cout << &ins1 << endl;
  cout << &ins2 << endl;
}

nested class having ptr to outer-class Object]java,c#,c++

Usually java is cleaner than c++ and c#. However, in this case I believe java is the Least clean.

Java “non-static nested class” feature is not “embraced” by Microsoft. All c# nested classes are static and can’t access non-static fields of the enclosing class.

C++ doesn’t support java style inner class either. See http://bigblog.tanbin.com/2009/03/nonstatic-nested-class-has-no-pointer.html

subverting – multiple inheritance #my take

!! With MI, the “this” pointer field is not always identical between the Base object and the Derived object.
** Remember a primitive technique of a home-made “class” is a C struct + a self-pointer field [2]. Similarly, In Python methods, “self” must be the first argument…

[2] I don’t think we should add a 32-bit field to each instance! I guess compiler can help make do without it.

!! In a Derived instance, there’s not always a single Base1 instance.
** Basic example — D extends C1 and C2, which both extend B.
** Even if C1 and C2 both virtually extend B, if C3 extends B without “virtual”, D instance still embeds 2 B instances.

!! Casting within inheritance hierarchy doesn’t always maintain the address held inside a Derived pointer or Base pointer. SI — always. ARM P 221 says “with MI, casting changes the value of a pointer”.

!! Each Derived instance uses more than one vtbl. If D inherits from B1 and B2, then that instance uses two vtbl’s. See ARM P230.

!! AOB (see other posts) assumes a 0 “delta” but in MI, delta is introduced because it’s not 0! ARM P222

— Below rules still holds,  in SI and MI —
dtor sequence is the exact reverse of ctor sequence which is BCDC (see other posts)