CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    55

    question about Function { What is defernce if I type int AA(5) or AA(5);

    Kindly See this code
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace System;
    using namespace std;
    int   AA(int a=0);
    int *pAA(int c=0);
    
    class CBox
    {
    public :
    	CBox()  //// Constructor
    
    	{
    		cout <<endl<< "Constractor Start";
    		cout <<endl<<"-------------------------";
    	}
    
    	~CBox()   ////destructor
    	{
    		cout<< endl <<" Destructor Start ";
    		cout<<endl <<"------------------------------";
    		
    };
    
    
    int main(void)
    {
    int (*pAA)(int ) = AA;
    	cout <<endl<< "1. Start Main Function";
    	cout <<endl<<"--------------------------------";
    
        int AA(5);  /// Here is My Question
        cout <<endl<<" The pointer to function  AA () : "<< pAA;
        return 0;
    
    }
    
    int AA(int  k)
    {
    	
    	cout <<endl<<" Start Function AA";
    	cout <<endl<<"---------------------------------";    
        CBox Box1;
        CBox Box2;
       return 0;
    }
    My Question is :
    When I wrote the line code :
    Code:
    AA(5);
    the function started and created two classes Box1 & Box2;

    But when I wrote the same code :
    Code:
    int AA(5);
    The Function did not work
    Kindly Expalin.

    Regards.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,448

    Re: question about Function { What is defernce if I type int AA(5) or AA(5);

    The first one is a function call (as you've seen yourself). It's perfectly legal to call a function that returns something (more precisely: has a non-void return type) like one that doesn't (i.e. has return type void). In this case the returned value is simply ignored. The opposite is not allowed, though, i.e. assigning a void return to a variable, passing it as a parameter to another function or using it in an expression. (Wouldn't make much sense anyway.)

    The second one, OTOH, is a function declaration, though an invalid one, because there's an int literal inside the parentheses where actually an optional list of parameter types with optional parameter names should be.

    For reference: http://www.cplusplus.com/doc/tutorial/functions/

    Now this is native C++ again, BTW...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,850

    Re: question about Function { What is defernce if I type int AA(5) or AA(5);

    Quote Originally Posted by Eri523 View Post
    The second one, OTOH, is a function declaration, though an invalid one, because there's an int literal inside the parentheses where actually an optional list of parameter types with optional parameter names should be.
    The line defines a variable called AA of type int that is initialized to the value 5.
    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

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,448

    Re: question about Function { What is defernce if I type int AA(5) or AA(5);

    Quote Originally Posted by D_Drmmr View Post
    The line defines a variable called AA of type int that is initialized to the value 5.
    D'oh! That's one of my pet gotchas... I remember some threads from the Non-VC++ section proving that I'm not the only one to fall victim to that, though...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    55

    Re: question about Function { What is defernce if I type int AA(5) or AA(5);

    Thank you D_Drmmr & Eri523 for perfect answers ..
    Kindly I have one more issue.. related to constructor & Destructors...
    I know we initate the objects from class .. for example if I have class = Box , now I can create many objects from this class say Box1, Box2...

    this simply I take part of memory to proccess these data .. finally to release memory I use destructor to free memory..

    By The same way.. If I use one Public Function (Not related to any class)...
    and this function say has 10 Variables A1,A2,A3..A10..
    Now after using this function .. can I release the memory by using destructor for this function...
    or destructors & Constructors are applicable only for Class Function .. and if yes Why..??

    Regards

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,448

    Re: question about Function { What is defernce if I type int AA(5) or AA(5);

    Quote Originally Posted by mecheil.edwar View Post
    By The same way.. If I use one Public Function (Not related to any class)...
    and this function say has 10 Variables A1,A2,A3..A10..
    Now after using this function .. can I release the memory by using destructor for this function...
    or destructors & Constructors are applicable only for Class Function .. and if yes Why..??
    Looks like you're talking about local variables (to the function). These variables get constructed when execution reaches the point of their declaration in the function's code. Then, when the function ends, the variables go out of scope, which is the time when they implicitly get destroyed. Explicit destruction using delete usually is only needed when you have dynamically allocated the variabes using new and stored their address in a raw pointer.

    In this respect free functions are not different from non-static or static class member functions.

    Perhaps the following program can demonstrate the above a bit. (Note that despite the fact that it is required to exist, plays a certain role in program start-up and a few syntactic peculiarities are in effect for it, main() is just an ordinary function.)

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class A
    {
    public:
      A()
      {
        cout << "Default-constructing A instance" << endl;
      }
    
      A(const string &str) : m_strPassedIntoCtor(str)
      {
        cout << "Constructing A instance: " << str << endl;
      }
    
      ~A()
      {
        cout << "Destroying A instance";
        if (!m_strPassedIntoCtor.empty())
          cout << ": " << m_strPassedIntoCtor;
        cout << endl;
      }
    
    private:
      string m_strPassedIntoCtor;
    };
    
    class B
    {
    public:
      B() : m_a1("member of B"), m_a2("another member of B")
      {
        cout << "Constructing B instance" << endl;
      }
    
      ~B()
      {
        cout << "Destroying B instance" << endl;
      }
    
    private:
      A m_a1, m_a2;
    };
    
    int main()
    {
      cout << "main() entered, constructing four instances of A..." << endl;
      A a1, a2, a3, a10;
      cout << "Done constucting A instances, constructing instance of B..." << endl;
      B b;
      cout << "Done constructing B instance, leaving main()..." << endl;
      return 0;
    }
    And this is the output generated by the program:

    Code:
    main() entered, constructing four instances of A...
    Default-constructing A instance
    Default-constructing A instance
    Default-constructing A instance
    Default-constructing A instance
    Done constucting A instances, constructing instance of B...
    Constructing A instance: member of B
    Constructing A instance: another member of B
    Constructing B instance
    Done constructing B instance, leaving main()...
    Destroying B instance
    Destroying A instance: another member of B
    Destroying A instance: member of B
    Destroying A instance
    Destroying A instance
    Destroying A instance
    Destroying A instance
    Drücken Sie eine beliebige Taste . . .
    Note that the destructor of B does not explicitly destroy the two instances of A held by the B object. Their destructors are called implicitly, whether B has its own explicitly defined destructor or not. (The destructors in this program merely serve demonstrational purposes anyway. They don't have any substantial functionality.) The same applies to the string instance held by the A objects, but here it's also important that it implicitly gets default-constructed upon default-construction of A instances, so it has a defined initial state. If it were a primitive type like int or anything else that doesn't have a default constructor, we'd need to explicitly initialize it, otherwise it would start out in an undefined state.

    HTH

    The correct term is "instantiate" instead of "initiate", BTW.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

+ Reply to Thread

Bookmarks

Posting Permissions

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



HTML5 Development Center

Click Here to Expand Forum to Full Width