Anonymous class
-
Hello, I know it`s kind of C++ question, but since Qt is entirely C++, I hope to find the answer here. This is my idea and my code:
AnonymousClass.h
@
class AnonymousClass {public:
Callbacks* factories;
AnonymousClass(Callbacks* cb);
AnonymousClass();
virtual ~AnonymousClass()=0;
virtual void say()=0;
};
@And somewhere in the program:
@
AnonymousClass* setAClass() {struct ooof : AnonymousClass { public: ooof(){ std::cout << "I am magic...\n"; } ~ooof() { std::cout << "I am dying...\n"; } void say() { std::cout << " I am anonymous ooof(). Hello!\n"; } }; return (ooof*) new ooof;
}
@So, when I compile, there are no errors. But I expect the couts() to be called, and the say() to be available in this fashion:
@
...
AnonymousClass* speaker = setAClass();
speaker->say(); //expected "I am anonymous ooof()"
@
However, it did not work. Please tell me where I got wrong. Also I can`t write back these days since my net sucks. I used a friendly PC to post here. Thanks in advance. -
I tried to compile your code, but the compiler complains that it doesn't know "Callbacks".
-
It works as expected on Linux g++ 4.8.3
I removed the references to Callbacks and added the implementations for ctor and dtor.@
class AnonymousClass
{
public:
AnonymousClass()
{
std::cout << FUNCTION << "\n";
}virtual ~AnonymousClass() { std::cout << __FUNCTION__ << "\n"; } virtual void say() = 0;
};
AnonymousClass* setAClass()
{
class ooof : public AnonymousClass
{
public:
ooof()
{
std::cout << "I am magic...\n";
}~ooof() { std::cout << "I am dying...\n"; } void say() { std::cout << " I am anonymous ooof(). Hello!\n"; } }; return (ooof*)new ooof;
}
int main()
{
AnonymousClass* speaker = setAClass();
speaker->say(); //expected "I am anonymous ooof()"
delete speaker;
return 0;
}
@And output is
@
AnonymousClass
I am magic...
I am anonymous ooof(). Hello!
I am dying...
~AnonymousClass
@What kind of problem do you have ?