I have a very basic code keeping in mind of java. I made a Object and Class class but in Template.
Object.hpp
#ifndef _OBJECT_HPP_
#define _OBJECT_HPP_
namespace library{
template<class T> class Object;
template<class T> class Class;
class Uint_32;
template<class T>
class Object{
public:
const static Uint_32& UNIQUEID;
private:
const Class<T>& myClass;
const static Class<T>& ref;
protected:
Object(Class<T>& myReference);
Object();
};
}
#endif
Object.cpp
#include "include//lang//template//Object.hpp"
#include "include//lang//template//Class.hpp"
#include "include//lang//Uint_32.hpp"
#include "iostream"
using namespace std;
using namespace library;
template<class T>const Uint_32& Object<T>::UNIQUEID=Uint_32(1);
template<class T>const Class<T>& Object<T>::ref=Class<T>();
template<class T>
Object<T>::Object(Class<T>& myReference):myClass(myReference){cout<<"
checking ";}
template<class T>
Object<T>::Object():myClass(ref){cout<<"ohk";}
Class.hpp
#ifndef _CLASS_HPP_
#define _CLASS_HPP_
#include"include//lang//Object.hpp"
namespace library{
template<class T>
class Class:public virtual Object<T>{
public:
Class();
const static Uint_32& UNIQUEID;
};
}
#endif
Class.cpp
#include "include//lang//template//Class.hpp"
#include "include//lang//Uint_32.hpp"
using namespace library;
template<class T>const Uint_32& Class<T>::UNIQUEID=Uint_32(2);
template<class T>
Class<T>::Class():Object(*this){
cout<<" hello ";
}
Uint_32.hpp
#ifndef _UINT_32_HPP_
#define _UINT_32_HPP_
#include "include//lang//Class.hpp"
#include "include//lang//Operators.hpp"
namespace library{
class Uint_32:public virtual Class<Uint_32>{
public:
Uint_32();
Uint_32(const int&&);
friend Uint_32& operator+(const Uint_32& a,const Uint_32& b);
friend Uint_32& operator<<(const Uint_32& a,const int& b);
const static Uint_32& UNIQUEID;
private:
int value;
};
}
#endif
Uint_32.cpp
#include "include//lang//Uint_32.hpp"
using namespace library;
const Uint_32& Uint_32::UNIQUEID=Uint_32(3);
Uint_32::Uint_32():Class<Uint_32>(){
value=0;
cout<<" here ";
}
Uint_32::Uint_32(const int&& val):Class<Uint_32>(){
value=val;
cout<<" there ";
}
t1.cpp
#include "include//lang//Uint_32.hpp"
using namespace library;
int main()
{
cout<<"\n";
Uint_32 a,b;
return 0;
}
Compile Command :
g++ -std=c++14 -I. -c src//lang//Uint_32.cpp -o obj//lang//Uint_32.o
g++ -std=c++14 -I. src//test//t1.cpp obj//lang//Uint_32.o -o bin//test
there is no compilation error for now at last. I have one more file with operators.hpp that just contain a template definition for every operator.
OUTPUT when I run the executable I get the following output and I probably cannot understand why? I tried every possible way to know. I also run over different system with different version.
ohk hello there checking hello
ohk hello here ohk hello here
What is happening here? Why my inheritance doesn't call correctly? I know I should not pass this pointer as it's not safe but I think I don't have alternative.
My problems
Object<T>::Object(Class<T>& myReference)
is being called only once but that should be called thrice.- there are four object creation in my point it must be either 3 or 5 (a and b in t1.cpp and UNIEQUEID initialisation in every class.
- why this is not working in Class.cpp file in constructor call?
- Is there any way I can check if I can make Object class to call
Object<T>::Object()
constructor so that T = Object class?