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.

How about “cin<<” ? I think the technique is valuable

  • when you have pre-agreed mixed content like integers, floats, strings
  • when you have a stream of integers or doubles

2 simple singleton classes – c++

First design is the static factory method getInstance(). Second design uses a local static object, based on [[eff C++]] P 222.

#include <iostream>
#include <string.h>
using namespace std;

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

///////////// 2nd design uses a static local object
class SimpleSingleton {
 SimpleSingleton() {
  cout << "SimpleSingleton()\n";
 }
 SimpleSingleton(SimpleSingleton const &);
 SimpleSingleton& operator=(SimpleSingleton const &);
public:
 static SimpleSingleton& get_instance() {
  static SimpleSingleton instance;
  return instance;
 }
};

int main() {
 StaticFieldSingleton<float>::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)

returning handle to class internal data #effC++

Never quizzed in QQ, but how about BP? Might be a code smell.

Returning a handle (ref/ptr) to a class’s internal data is an everyday reality and inevitable, but tricky (– like online shopping) [[EffC++]] has a nice chapter /devoted/ to this topic. I don’t have the book now so here are my personal views.

Note — in contrast returning by value (pbclone) is safe.

java programmers feel safe to return “internal data”. I know from experience it’s seldom an issue. Therefore most of those c++ issues below are somehow “fixed” in java, largely because java is a simple_clean language, compared to the other 2 superpowers — C# and c++.

Issue: java immutable classes should not expose fields (except final primitives??). Ditto for c++, but more fine prints … C++ immutable is almost impossible. Constness can be cast away. A double-pointer field… (Java “final myField” means a field of either const-object or const-pointer to non-const object)

Issue: if an object is const or a method is const, then exposing that handle can break that “promise”. Apparently, c++ compiler can’t detect the “leak”. Java doesn’t make this kind of const “promise”

Issue: Once a handle is returned and “cached”, the original object had better (but often can’t) stay clear of the bulldozer, either on the stack or the heap. Fixed in java…(obviously)

Issue (java too): if a field is an integral and inseparable half of a pair which must be manipulated as a whole, then exposing the field alone is questionable. (Let’s not go into details) Any example?

c++ nested class experiment

#include
class Outer{
   int m_o;
   public:
   class Inner   {
      public:
        int m_i;
        Inner(){m_i=999;}
        Inner(Outer & m_outer, int x){
           m_outer.m_o = x; //against c++98
        }
   };
   Outer(int y)   {
       cout <m_inner.m_i<<" <—- this field is no-arg constructed.n";
       m_inner.m_i = y;
   }
   void Display()   {
       using namespace std;
       cout << m_o << endl
            << m_inner.m_i << endl;
   }
   Inner m_inner;
};
int main(){
   Outer    objOut(10); //inner == 10
   Outer::Inner objIn(objOut, 5); // objIn discarded, but objOut modified to 5
   objOut.Display();
   cin.get();
}