Map of the same class as data member
CPP
#include <iostream>
#include <map>
using namespace std;

class temp {
    public :std::map<int,temp> m;
};

int main () {
    temp t,t2,t3;
    cout<<sizeof(t)<<endl;
    t.m.insert(std::make_pair(1,t2));
    cout<<sizeof(t)<<endl;
    t.m.insert(std::make_pair(2,t3));
    cout<<sizeof(t)<<endl;
    return 0;
}

Q1. How the size of object "t" calculated?

Q2. If the Object of the same class is not possible because the size of the class is not known then why map with the same class object is allowed?
E.g.

class XYZ {
    XYZ a; // error , as size is unknown
    std::map<int,XYZ> m;// is allowed, why ?
}


 

Author: Rahul_Agrawal
Login to Answer
Sort By:
Oldest
|
Newest
|
Vote
|
My Answer