Results 1 to 5 of 5

Thread: c++ 11 - can i change functions in Global Scope section?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,897

    c++ 11 - can i change functions in Global Scope section?

    see these code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class test
    {
        public:
            virtual void ola()
            {
                cout <<"hi" << endl;
            }
    
            test()
            {
                //nothing;
            }
    };
    
    class test1 : public test
    {
        public:
    
            test1()
            {
                //nothing;
            }
    };
    
    void test1::ola()
    {
        cout << "hello" << endl;
    }
    
    
    int main()
    {
    
        //cout << test::a << endl;
        return 0;
    }
    i get 1 error the 'ola' isn't 'test1' member. so the virtual functions must be re-declared
    is there anyway for resolve these for avoid re-declare the functions?
    my objective is change their values in Global Scope
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    991

    Re: c++ 11 - can i change functions in Global Scope section?

    Code:
    oid test1::ola()
    {
        cout << "hello" << endl;
    }
    as you are defining the definition of the function ola() for class test1 separately from the declaration of the class test1 then the class test1 needs to have the function ola() declared.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class test
    {
        public:
            virtual void ola();
    
            test()
            {
                //nothing;
            }
    };
    
    class test1 : public test
    {
        public:
    		virtual void ola();
    
            test1()
            {
                //nothing;
            }
    
    };
    
    void test::ola()
    {
        cout << "hello from ola test" << endl;
    }
    
    void test1::ola()
    {
        cout << "hello from ola test1" << endl;
    }
    
    void virt(test *cs)
    {
    	cs->ola();
    }
    
    int main()
    {
    test *o = new test;
    test *o1 = new test1;
    
    	virt(o);
    	virt(o1);
    
        return 0;
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,897

    Re: c++ 11 - can i change functions in Global Scope section?

    Quote Originally Posted by 2kaud View Post
    Code:
    oid test1::ola()
    {
        cout << "hello" << endl;
    }
    as you are defining the definition of the function ola() for class test1 separately from the declaration of the class test1 then the class test1 needs to have the function ola() declared.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class test
    {
        public:
            virtual void ola();
    
            test()
            {
                //nothing;
            }
    };
    
    class test1 : public test
    {
        public:
    		virtual void ola();
    
            test1()
            {
                //nothing;
            }
    
    };
    
    void test::ola()
    {
        cout << "hello from ola test" << endl;
    }
    
    void test1::ola()
    {
        cout << "hello from ola test1" << endl;
    }
    
    void virt(test *cs)
    {
    	cs->ola();
    }
    
    int main()
    {
    test *o = new test;
    test *o1 = new test1;
    
    	virt(o);
    	virt(o1);
    
        return 0;
    }
    i'm sorry, but that solution don't resolve my problem
    i have read something, but i can't find the sample: the 'override' can resolve my problem?
    do you ever used VB6? i'm trying doing procedures\functions like the 'events'. be changed in Global Scope by instances names. but the C++ don't let me use intances in Global Scope... only when i declare them and inicialize them.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    991

    Re: c++ 11 - can i change functions in Global Scope section?

    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: c++ 11 - can i change functions in Global Scope section?

    Quote Originally Posted by 2kaud View Post
    Code:
    oid test1::ola()
    {
        cout << "hello" << endl;
    }
    as you are defining the definition of the function ola() for class test1 separately from the declaration of the class test1 then the class test1 needs to have the function ola() declared.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class test
    {
        public:
            virtual void ola();
    
            test()
            {
                //nothing;
            }
    };
    
    class test1 : public test
    {
        public:
    		virtual void ola();
    
            test1()
            {
                //nothing;
            }
    
    };
    
    void test::ola()
    {
        cout << "hello from ola test" << endl;
    }
    
    void test1::ola()
    {
        cout << "hello from ola test1" << endl;
    }
    
    void virt(test *cs)
    {
    	cs->ola();
    }
    
    int main()
    {
    test *o = new test;
    test *o1 = new test1;
    
    	virt(o);
    	virt(o1);
    
        return 0;
    }
    You dynamically allocated those objects but never delete them which is very bad. Also, why is the class that inherits test publicly have a virtual keyword on ola()?

    I would also not suggest mixing around prototypes and declarations like this. However, here is the solution you want:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class test
    {
      public:
        virtual void ola();
    };
    
    class test1 : public test
    {
      public:
        void ola();
    };
    
    void test::ola()
    {
      cout << "hello from ola test" << endl;
    }
    
    void test1::ola()
    {
      cout << "hello from ola test1" << endl;
    }
    
    int main()
    {
      test t;
      t.ola();
    
      test1 t1;
      t1.ola();
    }
    I was also confused on why you included <string>, so I have removed that as well.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width