Kunal Budhiraja
Kunal Budhiraja

Reputation: 59

Can we assign a function to a object in cpp?

class test 
{ 
    int a; 
    static int cnt; 
    public: 
    test() 
    { 
        a=0; 
        cout <<++cnt; 
    } 
    test( int p) 
    { 
        a=p; 
        cout <<++cnt; 
    } 
    ~test() 
    { 
        cout<<cnt--; 
    } 
}; 
int test::cnt; 
void main() 
{ 
    test ob,ob1(10); 
    ob = test(); 
    test(); 
} 

In this code snippet ob=test(); how ob can be assigned a function test.Test is a class and we are invoking it like a function.How can this be possible

Upvotes: 0

Views: 595

Answers (1)

jwdonahue
jwdonahue

Reputation: 6659

Functions return void, objects, references or pointers to objects, that can generally be assigned to variables in your program. In this particular case, you are calling the test class object constructors and possibly encountering undefined behavior on the final call. I need to investigate further on the possible UB, the C++ standard has changed twice since I last read it, VS-2017 may not be the best oracle, and my C++ foo is little weak.

As far as I recall, there's more than one way to initialize an object in C++ and your instructor has obviously given you an assignment to learn this first hand.

test ob; // Invokes default constructor on test
test ob(); // Invokes default constructor on test.
test ob = test::test(); // Invokes default constructor on test.

It's always good to experiment with your code and get it to output usable diagnostics. I tweaked it a bit to get more organized output and force the app to wait on user input prior to exiting. You should also learn to use your debugger. You can learn a lot by simply stepping through your own code.

#include <iostream>
#include <cstdlib>

class test
{
    int a;
    static int cnt;
public:
    test()
    {
        a = 0;
        cnt++;
        std::cout << cnt << std::endl;
    }
    test(int p)
    {
        a = p;
        cnt++;
        std::cout << cnt << std::endl;
    }
    ~test()
    {
        std::cout << cnt << std::endl;
        cnt--;
    }
};
int test::cnt = 0;

int main(void)
{
    {
        test ob; // test::cnt is incremented, 1 is displayed on the console.
        test ob1(10); // test::cnt is incremented, 2 is displayed on the console.
        ob = test::test(); // test::cnt is incremented, 3 is displayed on the console.
        // The following instantiates a temporary test object,
        // the constructor is called, but test::cnt is not incremented on my system.
        // Seems we might be in undefined behavior territory?
        test::test();
    }
    system("pause");
}

Notice that I added an additional context to 'main()'. You can't rely on destructors outputting anything to the console after the end of 'main()' which is where your objects are destructed. Moving all the objects into the additional {} context, forces them to be constructed and destroyed therein, allowing us to a complete picture of what's going on at our console output.

The output on my Windows box for the above code is:

1
2
3
3
3
3
2
1
Press any key to continue . . .

I expected a count to 4 and then a count down from 4. That last call is definitely confusing me. If nobody chimes in with a definitive explanation, I'll look into it as soon as I can.

Upvotes: 1

Related Questions