Re: Derived Inner Class ...
Am 21.07.2011 23:06, schrieb asif.lse:
Hi,
Is creating an inner class Inner (inside another enclosing class, that
is - e.g. SomeClass), which is itself a sub-class of another class
Base, valid in ISO C++? Here is some code that works in Visual C++
2010 Express:
What you are describing as "inner class" is formally a so-called "nested class" in C++.
------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
struct SomeBase
{
SomeBase(int someInt) : someInt_(someInt) {}
int someInt_;
};
struct SomeClass
{
SomeClass(int someInt) : someInt_(someInt) {}
//YOU CAN CREATE AN INNER CLASS WHICH IS ITSELF A SUBCLASS OF ANOTHER
CLASS - IS THIS ISO C++? SUPPORTED BY GCC?
struct SomeInner : public SomeBase
{
SomeInner(int someInt) : SomeBase(someInt) {}
void printSomeInt() {std::cout<< this->someInt_<< std::endl ;}
};
int someInt_;
};
This is fine by the language. Why do you believe that this could problematic?
struct SomeClass2
{
struct SomeInner2;
SomeClass2(int someInt) : someInt_(someInt) {}
int someInt_;
};
struct SomeClass2::SomeInner2 : SomeBase
{
SomeInner2(int someInt) : SomeBase(someInt){}
void someOtherFunction() {std::cout<< "hello world"; }
};
int main (int argc, char **argv)
{
SomeClass sc(9211);
SomeClass::SomeInner si(4209211);
si.printSomeInt();
SomeClass2::SomeInner2 si2(9211);
si2.someOtherFunction();
return 0;
}
------------------------------------------------------------------------------------------------------------------------------
This compiles/runs ok on gcc 4.6.0 as well. Is this valid C++?
Yes, this is portable C++.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]