[comp.lang.c++] A problem with an identification method.

gsanchez@greco.dit.upm.es (Gabriel Sanchez Gutierrez) (05/07/90)

/*-----*/

This is a little problem I have. I need a class Identif for asking to
the objects by its own class.

I use the Identif class for the questions about identification. Each class 
in the hierarchy defines his identifier like a const name[] . I want
that in the call to base class in the constructer, the assignment will
be produce.

The hierarchy.

Identif          A
              /     \
             B          C
           / | \
           D E F

The code is very simple and I think there is no problems in it.
Only one thing: the virtual inheritance of father class. The main
cause of this is to allow derived class to call the Identif
constructor directly, with his own identifier.

Well. This code, in one file (removing the includes, of course) runs
well (or if I put the constructor in the .hpp file), but if I split it
in various files this mechanism doesn`t work.

Why??.

--------------------------------------
// Version: g++ 1.36

// File: identif.hpp. 

#ifndef _Identif_H
   
#pragma once
#define _Identif_H 1

class Identif {
          const char *id;
 protected:
          Identif (const char *nom = "Noname");
 public  :
    const char*       iden     (void) const;
          void        print_id (void) const;
          int         The_same_class (const Identif& ii) const;
 };

#endif

/*----------*/

// File: identif.cc

#include <stream.h>
#include "identif.hpp"


Identif::Identif (const char *nom) : id(nom) 
              { cout << "Constructor: " << id << "\n";}

const
char*  Identif::iden     (void) const {return id;};
void   Identif::print_id (void) const
              { cout << " {Identify: " << id << "}\n";}
int    Identif::The_same_class (const Identif& ii) const
              { return  (iden() == ii.iden());}

/*----------*/

// File: father.hpp

#ifndef _Father_H

#pragma once
#define _Father_H 1

#include "identif.hpp"


class Father : public virtual Identif {
  public:
     Father ();
     virtual Father& operator = (const Father&);
};

#endif

/*---------*/


// File: father.cc

#include <stream.h>
#include "father.hpp"
#include "identif.hpp"

const char Father_class[] = "Father class";

Father::Father () : Identif(Father_class) { /* .. body ..*/ ;}

Father& Father::operator = (const Father& p)
{
   cout << "Father: operator =\n";
   this->print_id();
   p.print_id();
}

/*---------*/

// File: test.cc

#include "identif.hpp"
#include "father.hpp"


main()
{
 Father  p1,p2;

  p1=p2;

}