Please explain this output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <iostream>
using namespace std;

class AAString
{
public:
    AAString()
    {   
        cout << "AAString()" << endl;
    }   
    
    AAString(int i = 2)
    {   
        cout << "AAString(int)" << endl;
    }   

    ~AAString()
    {   
        cout << "Destroyed.." << endl;
    }   
};


int main()
{
    cout << "Started" << endl;
    AAString obj();
    cout << "object created." << endl;
    cout << "Address of the object:" << &adlkjlkf << endl;
    return 0;
}



Started
object created.
Address of the object:1


Please explain why this object is created on memory location 0x1.

Thanks
It's not.

There are a few things wrong with your code.

 
    AAString obj();


This doesn't create an object. It defines a function. Lose the parenthesis.


Also:
 
cout << "Address of the object:" << &adlkjlkf << endl;


I don't know what you expect adlkjlkf to be, but you never declared any object by that name.


This code wouldn't even compile.
I'm sorry that line should be

cout << "Address of the object:" << &obj<< endl;
AAString obj();??? If you are trying to create an object you should change it to AAString obj;
Why? I have used default value for overloaded constructor.

Therefore AAString(int i = 2) shouldn't be called when I create the object?

Also I need to know why it prints 1 for address of obj.
No. You should call it like AAString obj = AAString(); which is not correct for your code because it is not clear. May be you can try AAString obj = AAString(3) or deleting the default constructor.

Your code doesn't print address of an object because there is no object.

Edit: I missed a thing, if you delete AAString(); then AAString obj() should work if you give a value. If you want to use default value then you do AAString obj;
Last edited on
closed account (DSLq5Di1)
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.4
I changed the code to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string>
#include <iostream>
using namespace std;

class AAString
{
public:
    //AAString()
    //{ 
    //  cout << "AAString()" << endl;
    //} 
    AAString(int i = 8 ) 
    {   
        cout << "AAString(int)" << endl;
    }   
    ~AAString()
    {   
        cout << "Destroyed.." << endl;
    }   
};


int main()
{
    cout << "Started" << endl;
    AAString objct();
    cout << "object created." << endl;
    cout << "Address of the object:" << &objct << endl;
    return 0;
}


but no difference in the output
Started
object created.
Address of the object:1
Hmm i think because of the compiler you use, it doesn't recognize it. Try AAString objct = AAString();
closed account (z05DSL3A)
rajimrt,
AAString objct(); What do you think this line is doing? [Hint: it is not creating an object]
@Grey Wolf +1.

You keep trying the same thing. Look AAString objct(5); would work but AAString objct(); wouldn't work. If you want to use the default value you should write AAString objct = AAString();.
closed account (z05DSL3A)
to create an object of type int, with its default constructor, you would write:
int x;
if you wanted to create it with a value:
1
2
int x= 8;  // or
int y(8);


so for your class, creating an object with the default constructor is:
AAString objct;
if you wanted to create it with a value:
AAString objct(4);

If AAString objct() does not create an object of AAString what else does it do?

Please give me a reference to read about this.
Thank you firedraco, but your reference does not tell me why any of two constructors are not called when I use AAString objct()
closed account (z05DSL3A)
It declares a function called objct that returns an AAString.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2
Last edited on
why any of two constructors are not called when I use AAString objct()

Because it is creating a call to a function called objct() that returns data of type AAstring

EDIT:
Corrected a mistake.
Last edited on
*ahem*


I in my first reply wrote:

This doesn't create an object. It defines a function. Lose the parenthesis.


=P
Topic archived. No new replies allowed.