我如何使用虚拟方法在其嵌套类中覆盖它? (Visual Studio 2013 C ++) [英] How can I used a virtual method override it within its nested class? (Visual Studio 2013 C++)

查看:50
本文介绍了我如何使用虚拟方法在其嵌套类中覆盖它? (Visual Studio 2013 C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个有趣的参考(来自:如何从基类变量(C ++,Visual Studio 2013)访问公共嵌套类方法? [ 1 ] )关于关键字virtual的使用;所以我开始研究它,我发现:



虚拟内部课程? - C ++论坛 [ 2 ]



我试过把它复制到visual studio并编译它,它工作得很好;但是,我在代码中使用类而不是示例中的struct。现在,我认为只要我定义范围,struct和class的使用就会非常相似。由于懒惰,我将每个班级的范围设定为公开,以免招致任何处罚。但是,与 class 的代码不会编译并导致很多错误颜色:#007777> struct 编译好。



为了测试的目的,一切都可以考虑在一个文件。



有效的代码是:



I had gotten an interesting reference (From: How can I access public nested class methods from base class variables (C++, Visual Studio 2013)?[1]) about the use of the keyword virtual; so I started looking into it a bit, and I found:

Virtual Inner Classes? - C++ Forum[2]

I tried copying that into visual studio and compiling it, and it worked fine; however, I use classes in my code versus struct in the example. Now, I figured that the use of struct and class would be very nearly the same, as long as I define the scope. Being lazy, I set the scope for each class to be defined as public to not incur any penalties. However, the code with the class in it does not compile and induces a lot of errors, versus the code with struct compiles fine.

For the purposes of the test, everything can be considered in one file.

The code that works is:

//Example taken from : http ://www.cplusplus.com/forum/lounge/61329/
//and duplicated here only to test if structures are equivalent to classes with Visual Studio 2013 as well as what to do with noexcept

#include <iostream>
#include <memory>

namespace one
{
	struct A
	{
		struct B
		{
			virtual ~B() {}  //was virtual ~B() noexcept{}, but removed the noexcept keyword to get it to compile
			virtual void foo() = 0;
		};

		std::shared_ptr<B> bar(bool);
	};

	std::shared_ptr<A::B> A::bar(bool f)
	{
		struct C : A::B
		{
			C(int, int) {}
			virtual void foo() override { std::cout << "one::A::bar::C::foo\n"; }
		};

		if (f) return std::make_shared<C>(3, 4);

		else
		{
			struct D : C
			{
				D(int a, int b, int c) : C(a, b) {}
				virtual void foo() override { std::cout << "one::A::bar::D::foo\n"; }
			};
			return std::make_shared<D>(10, 20, 30);
		}
	}
}

int main()
{
	one::A a;
	auto p = a.bar(false);
	p->foo(); // prints one::A::bar::D::foo
}

#endif







无法编译的代码是:






The code that does not compile is:

//Example taken from : http ://www.cplusplus.com/forum/lounge/61329/
//and duplicated here only to test if structures are equivalent to classes with Visual Studio 2013 as well as what to do with noexcept

#include <iostream>
#include <memory>

namespace one
{
	struct A
	{
		struct B
		{
			virtual ~B() {}  //was virtual ~B() noexcept{}, but removed the noexcept keyword to get it to compile
			virtual void foo() = 0;
		};

		std::shared_ptr<B> bar(bool);
	};

	std::shared_ptr<A::B> A::bar(bool f)
	{
		struct C : A::B
		{
			C(int, int) {}
			virtual void foo() override { std::cout << "one::A::bar::C::foo\n"; }
		};

		if (f) return std::make_shared<C>(3, 4);

		else
		{
			struct D : C
			{
				D(int a, int b, int c) : C(a, b) {}
				virtual void foo() override { std::cout << "one::A::bar::D::foo\n"; }
			};
			return std::make_shared<D>(10, 20, 30);
		}
	}
}

/*Variation on namespace a from the example, using public classes in stead of struct*/
namespace two
{
	class A
	{
	public:
		class B
		{
		public:
			virtual ~B() {}  //was virtual ~B() noexcept{}, but removed the noexcept keyword to get it to compile
			virtual void foo() = 0;
		};

		std::shared_ptr<B> bar(bool);
	};

	std::shared_ptr<A::B> A::bar(bool f)
	{
		class C : A::B
		{
		public:
			C(int, int) {}
			virtual void foo() override { std::cout << "one::A::bar::C::foo\n"; }
		};

		if (f) return std::make_shared<C>(3, 4);

		else
		{
			class D : C
			{
			public:
				D(int a, int b, int c) : C(a, b) {}
				virtual void foo() override { std::cout << "one::A::bar::D::foo\n"; }
			};
			return std::make_shared<D>(10, 20, 30);
		}
	}
}


int main()
{
	one::A a;
	auto p = a.bar(false);
	p->foo(); // prints one::A::bar::D::foo
}

#endif





错误如下: c2243(有关详细信息,请参阅:编译器错误C2243 [ 3 ])





The errors are: c2243 (see for detail: Compiler Error C2243[3])

1>------ Build started: Project: bmpLoad, Configuration: Debug Win32 ------
1>  CDCDirectControls.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(291): error C2243: 'type cast' : conversion from 'two::A::bar::C *' to 'two::A::B *' exists, but is inaccessible
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(577) : see reference to function template instantiation 'std::_Ptr_base<_Ty>::_Ptr_base<two::A::bar::C>(std::_Ptr_base<two::A::bar::C> &&)' being compiled
1>          with
1>          [
1>              _Ty=two::A::B
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(577) : see reference to function template instantiation 'std::_Ptr_base<_Ty>::_Ptr_base<two::A::bar::C>(std::_Ptr_base<two::A::bar::C> &&)' being compiled
1>          with
1>          [
1>              _Ty=two::A::B
1>          ]
1>          c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(186) : see reference to function template instantiation 'std::shared_ptr<two::A::B>::shared_ptr<two::A::bar::C,void>(std::shared_ptr<two::A::bar::C> &&) throw()' being compiled
1>          c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(186) : see reference to function template instantiation 'std::shared_ptr<two::A::B>::shared_ptr<two::A::bar::C,void>(std::shared_ptr<two::A::bar::C> &&) throw()' being compiled
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(291): error C2243: 'type cast' : conversion from 'two::A::bar::D *' to 'two::A::B *' exists, but is inaccessible
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(577) : see reference to function template instantiation 'std::_Ptr_base<_Ty>::_Ptr_base<two::A::bar::D>(std::_Ptr_base<two::A::bar::D> &&)' being compiled
1>          with
1>          [
1>              _Ty=two::A::B
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(577) : see reference to function template instantiation 'std::_Ptr_base<_Ty>::_Ptr_base<two::A::bar::D>(std::_Ptr_base<two::A::bar::D> &&)' being compiled
1>          with
1>          [
1>              _Ty=two::A::B
1>          ]
1>          c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(196) : see reference to function template instantiation 'std::shared_ptr<two::A::B>::shared_ptr<two::A::bar::D,void>(std::shared_ptr<two::A::bar::D> &&) throw()' being compiled
1>          c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(196) : see reference to function template instantiation 'std::shared_ptr<two::A::B>::shared_ptr<two::A::bar::D,void>(std::shared_ptr<two::A::bar::D> &&) throw()' being compiled
1>  Generating Code...
1>  Skipping... (no relevant changes detected)
1>  bmpLoadView.cpp
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========





所有代码都自包含在一个文件中。



1)如何使用类重写代码?

2)为什么它适用于 struct 而不是 class ? (我认为除了范围之外两者是相同的,但是使用 public:说明符,它们的行为应该相同。)



其他参考文献是:



虚拟内部课程? - C ++论坛 [ 3 ]



虚函数说明符 - cppreference .com [ 4 ]



All the code is self-contained in one file.

1) How the code need to get rewritten using classes?
2) Why does it work with struct but not with class? (I thought the two were identical except for scope, but with public: specifiers, they should act the same.)

Additional references are:

Virtual Inner Classes? - C++ Forum[3]

virtual function specifier - cppreference.com[4]

推荐答案

当您公开所有类的成员时,您忘记了默认情况下私有的基类的访问说明符。所以也要公开:

While you have made all the members of your classes public you forgot the access specifier for the base class which is private by default. So make that public too:
class C : public A::B
{
// ...
};

class D : public C
{
// ...
};


这篇关于我如何使用虚拟方法在其嵌套类中覆盖它? (Visual Studio 2013 C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆