如何从基类变量(C ++,Visual Studio 2013)访问公共嵌套类方法? [英] How can I access public nested class methods from base class variables (C++, Visual Studio 2013)?
问题描述
我有一个嵌套类
int testfunction()
{
CDCDirectControls :: Graphic :: Button Ÿ;
y.isNew.Set();
CDCDirectControls x1;
x1.Graphic.Button.isNew.Set(); //不编译错误
返回1;
}
我得到以下输出:
1> ------ Build build:Project:bmpLoad,Configuration:Debug Win32 ------
1> CDCDirectControls.cpp
1> c:\ users \stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93):warning C4832:token'。 '在UDT'CDCDirectControls :: Graphic'
1>之后是非法的。 c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(44):参见'CDCDirectControls :: Graphic'的声明
1> c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93):错误C2274:'function-style cast':非法作为右侧'。'运算符
1> c:\ usersrs \stephen\desktop\stephen\cppprojects \samplemfc \bmpload\bmpload \ cdcdirectcontrols.h(93):错误C2228:左边的'.Button'必须有class / struct / union
1> c:\ usersrs \stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93 ):错误C2228:'。isNew'左边必须有class / struct / union
1> c:\ usersrs \stephen \ desktop \stephen \ cppprojects \samplemfc \ bmpload \bmpload \cdcdirectcontrols .h(93):错误C2228:'。''的左边必须有class / struct / union
1>生成代码...
1>跳过...(未检测到相关更改)
1> bmpLoadView.cpp
==========构建:0成功,1个失败,0个最新,0跳过==========
我已将错误固定到标有不编译错误的行。
更新#1:
我在类中定义了变量itGraphic,itsButton等等然后它没有编译器错误就像这样工作。但有没有更好的方法来使用调用变量?是否有更好的语法?
否则,当我从cpp文件中拆分头时,我需要小心将变量定义放在cpp端。< br $> b $ b
int testfunction()
{
CDCDirectControls :: Graphic :: Button y;
y.isNew.Set();
CDCDirectControls x1;
x1.itsGraphic.itsButton.isNew.Set();
return 1 ;
}
问题:
1)如果有一个基类的变量,访问嵌套类方法的正确方法是什么?
1a)是否有方法独立于在类中定义变量? (我想可以定义一个指针;但是有什么比这更清晰吗?)
x1.itsGraphic-> itsButton-> isNew.Set(); //当itsGraphic被定义为类中Graphic的指针时,编译得很好,但有没有更简洁的方法从基类访问嵌套类方法?
2)是从基类变量引用嵌套类的唯一方法 - 创建变量然后访问变量?
参考文献:
嵌套类声明 [ 1 ]
初始化嵌套类 - C ++论坛 [ 2 ]
< blockquote>如果您有特定类型的对象,则无法访问从该对象派生的类的成员。
但是常见的情况是你有一个对象的引用应该在别处使用(例如作为参数传递给另一个函数)使用基类的类型。在这种情况下,您可以将传递的引用强制转换为实际类型或使用虚函数。
Casting( dynamic_cast转换 - cppreference.com [ ^ ])要求您知道实时类型的运行时类型信息(RTTI)(请参阅类型支持(基本类型,RTTI,类型特征)) - cppreference.com [ ^ ]和运行时类型信息 - MSDN [ ^ ])或在基地有一名成员对于每个派生类都是唯一的类。
使用虚拟函数是更智能的解决方案。
你的派生类应该提供一个函数 isNew()
,将其添加为虚拟(虚拟函数说明符 - cppreference.com [ ^ ] )到基类和派生类。如果基类是抽象的(不用作变量或成员),它可以是纯虚函数(参见抽象类 - cppreference .com [ ^ ])以确保它在所有派生类中实现。
I have a nested class
int testfunction() { CDCDirectControls::Graphic::Button y; y.isNew.Set(); CDCDirectControls x1; x1.Graphic.Button.isNew.Set(); // does not compile with error return 1; }
I get the following output:
1>------ Build started: Project: bmpLoad, Configuration: Debug Win32 ------ 1> CDCDirectControls.cpp 1>c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93): warning C4832: token '.' is illegal after UDT 'CDCDirectControls::Graphic' 1> c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(44) : see declaration of 'CDCDirectControls::Graphic' 1>c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93): error C2274: 'function-style cast' : illegal as right side of '.' operator 1>c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93): error C2228: left of '.Button' must have class/struct/union 1>c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93): error C2228: left of '.isNew' must have class/struct/union 1>c:\users\stephen\desktop\stephen\cppprojects\samplemfc\bmpload\bmpload\cdcdirectcontrols.h(93): error C2228: left of '.Set' must have class/struct/union 1> Generating Code... 1> Skipping... (no relevant changes detected) 1> bmpLoadView.cpp ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have pinned the error down to the line marked with "does not compile with error".
Update #1:
I defined variables inside the class itsGraphic, itsButton, and so forth and then it works like this without a compiler error. But is there a better way of doing it with the calling variable? Is there a better syntax?
Otherwise, when I split the header from the cpp file, I need to be careful to put the variable definition on the cpp side.
int testfunction()
{
CDCDirectControls::Graphic::Button y;
y.isNew.Set();
CDCDirectControls x1;
x1.itsGraphic.itsButton.isNew.Set();
return 1;
}
Questions:
1) If one has a variable of the base class, what is the proper way to access the nested class methods?
1a) Is there a method independent of defining a variable in the class? (I guess a pointer could be defined; but is there anything cleaner than that?)
x1.itsGraphic->itsButton->isNew.Set(); //compiles fine when itsGraphic is defined as a pointer to Graphic in the class, but is there a cleaner way to access the nested class methods from the base class?
2) Is the only way to reference the nested class from the base class variable - to create a variable and then access the variable?
References:
Nested Class Declarations[1]
Initializing nested classes - C++ Forum[2]
If you have an object of a specific type, you can not access members of classes that are derived from that object.
But a common situation is that you have a reference to an object that should be used elsewhere (e.g. passed as parameter to another function) using the type of a base class. In this case you can cast the passed reference to the real type or use virtual functions.
Casting (dynamic_cast conversion - cppreference.com[^]) requires that you know the real type by run-time type information (RTTI) (see Type support (basic types, RTTI, type traits) - cppreference.com[^] and Run-Time Type Information - MSDN[^]) or having a member in the base class that is unique for each derived class.
Using virtual functions is the smarter solution.
When your derived classes should provide a functionisNew()
, add that as virtual (virtual function specifier - cppreference.com[^]) to the base class and the derived classes. If the base class is abstract (not used itself as variable or member) it can be a pure virtual function (see Abstract Classes - cppreference.com[^]) to ensure that it is implemented in all derived classes.
这篇关于如何从基类变量(C ++,Visual Studio 2013)访问公共嵌套类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!