I have a nested class
int testfunction()
{
CDCDirectControls::Graphic::Button y;
y.isNew.Set();
CDCDirectControls x1;
x1.Graphic.Button.isNew.Set();
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();
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]