Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Anonymous class
Forum Updated to NodeBB v4.3 + New Features

Anonymous class

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 904 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    heatblazer
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • O Offline
      O Offline
      ondrejandrej
      wrote on last edited by
      #2

      I tried to compile your code, but the compiler complains that it doesn't know "Callbacks".

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #3

        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 ?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved