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.
Re: question about Function { What is defernce if I type int AA(5) or AA(5);
Originally Posted by Eri523
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
Re: question about Function { What is defernce if I type int AA(5) or AA(5);
Originally Posted by D_Drmmr
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.
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..??
Re: question about Function { What is defernce if I type int AA(5) or AA(5);
Originally Posted by mecheil.edwar
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.
Bookmarks