little explanation-operator () overload

This is a discussion on little explanation-operator () overload within the C++ Programming forums, part of the General Programming Boards category; Hello, can anybody explain me the difference between these two memory allocations: Code: #include <iostream> using namespace std; class Test ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    little explanation-operator () overload

    Hello,
    can anybody explain me the difference between these two memory allocations:
    Code:
    #include <iostream>
    using namespace std;
    
    class Test
    {
    	/*something here*/
    public:
    	void operator() ()
    	{
    		cout<< "Operator()";
    	}
    };
    
    int main()
    {
    	Test * pT = new Test;
    	Test * pT2 = new Test();
    }
    I thought that that there is no difference except in case where operator() is overloaded but it seems that isn't true.
    I need explanation about difference between Test and Test() in allocation.
    Thanks
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    Test * pT = new Test;
    Test * pT2 = new Test();
    I think they are the same thing, the overloaded operator() will be used with the variable itself, like:
    Code:
    Test b;
    Test* a = new Test();
    ...
    b();
    (*a)();
    I think the overloaded operator() will only work like that. I'm not sure of this though.
    Last edited by LinuxCoder; 04-11-2006 at 01:16 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This should indicate what is happening:
    Code:
    #include <iostream>
    using namespace std;
    
    class Test
    {
    	/*something here*/
    public:
    	
    	Test()
    	{
    		cout<<"default constructor called."<<endl;
    	}
    	void operator() ()
    	{
    		cout<< "Operator()";
    	}
    };
    
    int main()
    {
    	Test * pT = new Test;
    	Test * pT2 = new Test();
    
    	(*pT)();
    
    	delete pT;
    	delete pT2;
    	
    	return 0;
    }
    Last edited by 7stud; 04-11-2006 at 02:05 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Note that there is a difference between these two:
    Code:
    Test T1;
    Test T2();

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Note that there is a difference between these two:

    how so?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Sebastiani
    >> Note that there is a difference between these two:

    how so?
    becuase the second is a tricky b@$tard!

    Code:
    class X
    {};
    
    X anX; // constructs an object of type X, right?
    
    X anotherX(); // does the same, just explicitly calls the default constructor, correct?
    NOPE!

    what if I type
    Code:
    X getX(); // obviously a function declaration returning X
    // NOT A INSTANCE OF AN OBJECT!
    I didn't actually believe this until I tried it. Scott Meyers calls it "C++'s most vexing parse"

    BTW you can get around this, by requiring functions with no arguements to take void in your coding standard.
    e.g.
    Code:
    void DoStuff(void);
    I actually think there should be a compiler switch to require this, at least as a warning.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> BTW you can get around this
    I'm not sure how the coding standard would help there, since you generally make that mistake attempting to create the variable (with the empty parentheses) rather than when trying to create a function.

    A compiler warning might be nice, though.

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved
    >> BTW you can get around this
    I'm not sure how the coding standard would help there, since you generally make that mistake attempting to create the variable (with the empty parentheses) rather than when trying to create a function.

    A compiler warning might be nice, though.
    You could setup splint to require all parameterless functions to require (void), which would be a start.

    I guess it doesn't really help that much.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Scott Meyers calls it "C++'s most vexing parse"

    right, I overlooked that. of course the ambiguity would only apply to objects declared outside any function.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Sebastiani
    >> Scott Meyers calls it "C++'s most vexing parse"

    right, I overlooked that. of course the ambiguity would only apply to objects declared outside any function.
    no, unforunatly it doesn't.

    for the following code
    Code:
    class X
    {
    public:
    	void DoStuff(void)
    	{
    	}
    };
    void func(void) // following my own coding standard :-)
    {
    	X x();
    	x.DoStuff();
    }
    VC7.1 gives the following error:
    error C2228: left of '.DoStuff' must have class/struct/union type
    type is 'overloaded-function'

    if you don't have the call to DoStuff you get the following warning (on level 4)
    'X x(void)': prototyped function not called (was a variable definition intended?)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I was completely ignorant of that - I didn't even know the language allowed function prototypes within functions. IMO a completely ridiculous feature: not only does it degrade the overall sensibility of the language, there probably aren't many people that actually use it!

    thanks for pointing that out, guys.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Sebastiani
    I didn't even know the language allowed function prototypes within functions. IMO a completely ridiculous feature: not only does it degrade the overall sensibility of the language, there probably aren't many people that actually use it!
    'Tis merely limiting the scope. If you only want a function to be able to used in one particular function, rather than being available to every function in a module, then that is a pretty legitimate use. Just like with "global" variables.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I thought that was what namespaces were for.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I thought namespaces were for avoiding clashes between identifiers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Private/Public Classes, explanation please in context
    By porsche911nfs in forum C++ Programming
    Replies: 9
    Last Post: 04-22-2009, 08:37 PM
  2. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 08:17 PM
  3. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  4. basic linked list declaration..need explanation
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-07-2002, 05:55 PM
  5. Explanation!!!
    By MITCHELL in forum Windows Programming
    Replies: 4
    Last Post: 10-25-2001, 01:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21