我有一个非常基本的代码,记住了Java。我创建了一个对象和类类,但在模板中。
对象.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
对象.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";}
类.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
类.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;
}
编译命令:
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
现在终于没有编译错误了。我还有一个带有 operator.hpp 的文件,它只包含每个运算符的模板定义。
输出当我运行可执行文件时,我得到以下输出,我可能不明白为什么?我尝试了所有可能的方法来知道。我还运行不同版本的不同系统。
ohk hello there checking hello
ohk hello here ohk hello here
这是怎么回事?为什么我的继承不能正确调用?我知道我不应该传递这个指针,因为它不安全,但我认为我没有其他选择。
我的问题
Object<T>::Object(Class<T>& myReference)
只被调用一次,但应该被称为三次。- 在我看来,有四个对象创建,它必须是 3 或 5(t1 中的 a 和 b.cpp以及每个类中的 UNIEQUEID 初始化。
- 为什么这在构造函数调用中的 Class.cpp 文件中不起作用?
- 有什么方法可以检查我是否可以使 Object 类调用构造函数
Object<T>::Object()
以便 T = Object 类?
您正在使用虚拟继承。派生最多的类负责初始化其所有虚拟基类。
当你写的时候
Uint_32::Uint_32(const int&& val):Class<Uint_32>(){ ... }
您似乎希望构造函数调用Class<Uint_32>()
构造函数Uint_32
进而调用Object(*this)
。但事实并非如此。由于Object
是一个虚拟基类,Uint_32
负责初始化它,而不是Class
。由于Uint_32
没有在其初始值设定项列表中提及它,因此使用了Object
的默认构造函数(而不是单参数构造函数(。
对Object(Class<T>&)
的那一声呼唤来自template<class T>const Class<T>& Object<T>::ref=Class<T>();
。这是将Class
实例化为派生最多的对象(而不是另一个对象的基类子对象(的唯一位置,这使得它负责调用构造函数Object
它与Object(*this)
一起调用。
你如何计算四个实例?在您显示的输出中,单词there
出现一次,单词here
两次,总共构造了三个Uint_32
实例。这些是a
,b
和Uint_32::UNIQUEID
。
不确定我是否理解你的问题 4.你是问你是否可以写,比如说,
Object<Object<int>> obj;
?我不明白为什么不这样做,但你可以很容易地尝试一下,亲眼看看。