级联继承中返回当前继承层类型的对象

<pre name="code" class="plain">
// cascadederivationreturntypeofcurrentderivationlevel.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

template <class T>
class A
{
public:
	// 以下代码C2355
	// 1 try
	//static auto Empty()->decltype(*this) // C2355 decltype(*this) 在这个类中还没有定义
	//{
	//	this->typeid l; // C2355 decltype(*this) 在这个类中还没有定义
	//	return l;
	//}
	// 2 try
	//static auto Empty()->decltype(*this) // C2355 decltype(*this) 在这个类中还没有定义
	//{
	//	decltype(*this) l; // C2355 decltype(*this) 在这个类中还没有定义
	//	return l;
	//}
	// 3 try
	//auto Empty()->decltype(*this) // C2355 decltype(*this) 在这个类中还没有定义
	//{
	//	return *this;
	//}
	// 以下代码在某种程度上可以运行
	//template <class T>
	//static auto Empty()->decltype(*this)
	//{
	//	this->typeid<T> l;
	//	return l;
	//}
	// 原因:虽然 this 能够被运行时推断因此可以被认为已知,但是 this 的类型不能够被运行时推断因此不能够被认为已知,所以 this (的类型)被认为没有定义

	// 以下代码可行
	// 1 okay
	//template <class T> // 函数模板
	//static c Empty()
	//{
	//	T l;
	//	return l;
	//}
};

template <class T>
class B : public A<T>
{
public:
	// 以下代码可行
	// 2 okay
	static B<T> Empty()
	{
		B<T> l;
		return l;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	// 以下代码C2355
	// 1&2 try
	//B<int> b = B<int>::Empty();
	// 3 try
	//B<int> type;
	//B<int> b = type.Empty();
	
	// 以下代码可行
	// 1 okay
	//B<int> b = B<int>::Empty<B<int>>();
	// 2 okay
	B<int> b = B<int>::Empty();

	return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值