about class's and '::' operator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: about class's and '::' operator

  1. #1
    Join Date
    Apr 2009
    Posts
    596

    about class's and '::' operator

    see these code(i know that isn't correct):

    Code:
    class test
    {
          public:
               void Printed(){};
               void write(string a)
               {
                        cout << a;
                        Printed();
               }
    };
    
    
    test a;
    
    void a::Printed()
    {
         cout << "\nmessage printed\n";
    }
    
    int main()
    {
          a.write("hello world");
          return 0;
    }
    how can i change the Printed() for be valid with '::' with an object?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,998

    Re: about class's and '::' operator

    Quote Originally Posted by Cambalinho View Post
    how can i change the Printed() for be valid with '::' with an object?
    You don't. '::' is the scope resolution operator. You can only use it after a namespace or class/struct name to specify the scope in which the subsequent identifier should be looked up.
    Printed() is a member function of test. To implement that outside the class definition, you need to specify that you mean the Printed member function of test, rather than a global function called Printed. You do that by writing test::Printed.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2009
    Posts
    596

    Re: about class's and '::' operator

    Quote Originally Posted by D_Drmmr View Post
    You don't. '::' is the scope resolution operator. You can only use it after a namespace or class/struct name to specify the scope in which the subsequent identifier should be looked up.
    Printed() is a member function of test. To implement that outside the class definition, you need to specify that you mean the Printed member function of test, rather than a global function called Printed. You do that by writing test::Printed.
    i understand what you mean, but i need 1 way for change the function with object name and outside of main(instead use '::', i can use the dot... but try understand what i mean(sorry my english)

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,998

    Re: about class's and '::' operator

    Quote Originally Posted by Cambalinho View Post
    i understand what you mean, but i need 1 way for change the function with object name and outside of main(instead use '::', i can use the dot... but try understand what i mean(sorry my english)
    Maybe you should try to describe your problem, rather than describing which solution you think will work.
    At a higher level, what do you want to do?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Apr 2009
    Posts
    596

    Re: about class's and '::' operator

    Quote Originally Posted by D_Drmmr View Post
    Maybe you should try to describe your problem, rather than describing which solution you think will work.
    At a higher level, what do you want to do?
    do you know VB code? so i need create objects that can change the functions(events) outside of main.
    sorry, i understand the C++ isn't realy prepared for these.... but i need do something like these. i can use the directive, but i must re-declare the virtual functions.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,992

    Re: about class's and '::' operator

    A class function is specified to be part of a class. Every instance of that class has the same set of functions. A class instance cannot have a different function from another class instance. Even with polymorphism and virual functions, all of the instances of the same derived class have the same functions.

    From your example, you can do something like this

    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    class test
    {
    public:
    	void Printed();
    	void write(string a)
    	{
    		cout << a;
    		Printed();
    	}
    	void SetText(const string& txt) {
    		text = txt;
    	}
    
    private:
    	string text;
    };
    
    void test::Printed()
    {
         cout << text;
    }
    
    int main()
    {
    test a;
    	
    	a.SetText("\nMessage printed\n");
    	a.write("hello world");
    	return 0;
    }
    where for each instance of the class you can specify some data.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,638

    Re: about class's and '::' operator

    the Printed() is a member function of the class named 'test'
    so the function definition needs to be test::Printed()

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: about class's and '::' operator

    Quote Originally Posted by Cambalinho View Post
    do you know VB code? so i need create objects that can change the functions(events) outside of main.
    sorry, i understand the C++ isn't realy prepared for these.... but i need do something like these. i can use the directive, but i must re-declare the virtual functions.
    That doesn't really make sense. All C++ programs start with main, which in turn calls other functions. "Objects that can change the functions" also doesn't make sense. What do you mean "change the functions"?

    You may be talking about static functions, which are members of a class that can be called without an instance of the class, but it's really hard to tell what you're trying to do.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,998

    Re: about class's and '::' operator

    Quote Originally Posted by Cambalinho View Post
    do you know VB code? so i need create objects that can change the functions(events) outside of main.
    sorry, i understand the C++ isn't realy prepared for these.... but i need do something like these. i can use the directive, but i must re-declare the virtual functions.
    What you describe is how you would solve your problem in VB (at least, I assume; I don't really understand what you want to do). That's not useful for solving a problem in C++, because C++ is not at all like VB. If you explain your problem - not your solution - then we can help you find a solution in C++.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,992

    Re: about class's and '::' operator

    Quote Originally Posted by GCDEF View Post
    That doesn't really make sense. All C++ programs start with main, which in turn calls other functions. "Objects that can change the functions" also doesn't make sense. What do you mean "change the functions"?

    You may be talking about static functions, which are members of a class that can be called without an instance of the class, but it's really hard to tell what you're trying to do.
    I think he's trying to describe/implement instance based polymorphism as opposed to class based polymorphism - but that isn't possible in the c++ language as far as I am aware.
    Last edited by 2kaud; November 8th, 2013 at 06:50 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #11
    Join Date
    Apr 2009
    Posts
    596

    Re: about class's and '::' operator

    Quote Originally Posted by 2kaud View Post
    I think he's trying to describe/implement instance based polymorphism as opposed to class based polymorphism - but that isn't possible in c++ as far as I am aware.
    thanks for help me with the right terms

  12. #12
    Join Date
    Oct 2008
    Posts
    1,077

    Re: about class's and '::' operator

    the simplest way to implement "per instance member functions" is to use std::function ; eg, your code snippet would become

    Code:
    #include <iostream>
    #include <string>
    #include <functional>
    
    using namespace std;
    
    class test
    {
          public:
               std::function<void()> Printed;
    
               void write(string const& a)
               {
                        cout << a;
                        Printed();
               }
    };
    
    
    test a = { []
        {
            cout << "\nmessage printed\n";
        } };
    
    int main()
    {
          a.write("hello world");
          return 0;
    }
    if your compiler is not c++11-ready, you can use boost::function instead.

    Anyway, note that there are gazillions ways of binding runtime behaviour to a class instance, depending on your requirements. So, as others said, you'd better explaining what you're trying to do at a higher level, and no, writing code this way because "VB code does so" is not a good idea ...

  13. #13
    Join Date
    Apr 2009
    Posts
    596

    Re: about class's and '::' operator

    Quote Originally Posted by superbonzo View Post
    the simplest way to implement "per instance member functions" is to use std::function ; eg, your code snippet would become

    Code:
    #include <iostream>
    #include <string>
    #include <functional>
    
    using namespace std;
    
    class test
    {
          public:
               std::function<void()> Printed;
    
               void write(string const& a)
               {
                        cout << a;
                        Printed();
               }
    };
    
    
    test a = { []
        {
            cout << "\nmessage printed\n";
        } };
    
    int main()
    {
          a.write("hello world");
          return 0;
    }
    if your compiler is not c++11-ready, you can use boost::function instead.

    Anyway, note that there are gazillions ways of binding runtime behaviour to a class instance, depending on your requirements. So, as others said, you'd better explaining what you're trying to do at a higher level, and no, writing code this way because "VB code does so" is not a good idea ...
    what you did was initializate with constructor. but, i belive, theres another way, but i don't know what is
    i'm sorry but the '()' is used out of scope(too), so why i can't overload it for use it in these way?

  14. #14
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,638

    Re: about class's and '::' operator

    You can do 'instance based polymorphism' (is that even a standardized terminology) ?

    by using lambda's
    or by using function pointers
    or functors/ function objects
    or one of many other possible solutions.

    it just requires "a bit" of explicit code assistance. I.m.o. this is just "implementation, which made me confused when I read that "instance base polymorphism" as being a "real" thing.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center