Problem of new Object within a Class

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <string>
#include <list>

using namespace std;
class Type{
    protected:
        int id;
        string name;
        int lv;
        int upLvId;
    public:
        int getId();
        int setId(int id);
        string getName();
        int setName(std::string name);
        int getLv();
        int setLv(int lv);
        int getUpLvId();
        int setUpLvId(int id);
        ~Type();
};

int Type::setLv(int lv){
    this->lv = lv;
    return 0;
}
int Type::getLv(){
    return this->lv;
}
int Type::setUpLvId(int id){
    this->id = id;
    return 0;
}
int Type::getUpLvId(){
    return this->upLvId;
}
int Type::setId(int id){
    this->id = id;
    return 0;
}
int Type::getId(){
    return this->id;
}

int Type::setName(std::string name){
    this->name = name;
    return 0;
}
std::string Type::getName(){
    return this->name;
}
Type::~Type(){
    delete this;
}

class XmlUtils{
    public:
        XmlUtils();
        XmlUtils(string* xmlString);
        int parseType();
        int print();
        ~XmlUtils();
    private:
        string xmlString; 
};
XmlUtils::XmlUtils(){
    XmlUtils(NULL);
}

XmlUtils::XmlUtils(string* xmlString){
    try{
        if (xmlString == NULL){
            this->xmlString = "<Types name=\"Knowledge Types\" language=\"English\">";
            this->xmlString.append("<Type id=\"1\">");
            this->xmlString.append("<Name>Information Technology</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"2\">");
            this->xmlString.append("<Name>Music</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"3\">");
            this->xmlString.append("<Name>Health</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"4\">");
            this->xmlString.append("<Name>Business</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"5\">");
            this->xmlString.append("<Name>Spirital</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Types>");
        } else {
            this->xmlString = *xmlString;
        }
        cout << this->xmlString << endl;
        cout << endl;
    } catch (exception &e){
        cout << e.what() << '\n';
    }
    cout << "end constructor!!" << endl;
}
int XmlUtils::print(){
    cout << "print" << endl;
    return 0;
}
int XmlUtils::parseType(){
    cout << "xmlString: " << endl;
    return 0;
}
XmlUtils::~XmlUtils(){
    delete this;
};

class TypeBuilder{
    private:
        list<Type> engTypeList;
        list<Type> chiTypeList;
        XmlUtils* xmlUtils;
    public:
        TypeBuilder();
        int print();
        list<Type> getInstanceOfEngTypeList();
        list<Type> getInstanceOfChiTypeList();
        ~TypeBuilder();

};
TypeBuilder::TypeBuilder(){
    cout << "constructor" << endl;
    this->xmlUtils = new XmlUtils();
}
int TypeBuilder::print(){
    cout << "TypeBuilder::print()" << endl;
    return 0;
}
list<Type> TypeBuilder::getInstanceOfEngTypeList(){
    try{
        cout << "before xmlUtils" << endl;
        cout << "after xmlUtils " << endl;
       // this->xmlUtils->parseType();
        
    } catch (exception &e){
        cout << "TypeBuilder::getInstanceOfEngTypeList() - " << endl;
        cout << e.what() << '\n';
    }
    return engTypeList;
}
list<Type> TypeBuilder::getInstanceOfChiTypeList(){
    try{
    } catch (exception &e){
        cout << e.what() << '\n';
    }
    return chiTypeList;
}
TypeBuilder::~TypeBuilder(){
    cout << "destructor" << endl;
    delete this->xmlUtils;
    delete this;
}


class TestClass{
    private:
        int id;
        std::string name;
        int lv;
        int upLvId;
        list<Type> engTypeList;
        XmlUtils* xmlUtils;
    public:
        TestClass();
        int getId();
        void setId(int id);
        std::string getName();
        void setName(std::string name);
        int getLv();
        void setLv(int lv);
        int getUpLvId();
        void setUpLvId(int id);
        int print(string* test);
        ~TestClass();
};

TestClass::TestClass() {
    cout << "constructor" << endl;
    this->id = 3;
}

int TestClass::print(string* test){
    try{
        if (test == NULL){
            cout << "NULL" << endl;
        } else {
            cout << *test << endl;
        }
    } catch (exception &e){
        cout << e.what() << '\n';
    }
    return 0;
}
void TestClass::setLv(int lv){
    this->lv = lv;
}
int TestClass::getLv(){
    return this->lv;
}
void TestClass::setUpLvId(int id){
    this->id = id;
}
int TestClass::getUpLvId(){
    return this->upLvId;
}
void TestClass::setId(int id){
    this->id = id;
}
int TestClass::getId(){
    return this->id;
}
void TestClass::setName(std::string name){
    this->name = name;
}
std::string TestClass::getName(){
    return this->name;
}
TestClass::~TestClass(){
    delete this;
}



int main()
{
    //TypeBuilder *typeBuilder = new TypeBuilder();
    // typeBuilder->getInstanceOfEngTypeList();
    //typeBuilder->print();
    //delete typeBuilder;
    //std::cout << "object type -> name: " + typeBuilder->getInstanceOfEngTypeList();
    XmlUtils *xmlUtils = new XmlUtils();
    
    TestClass *testClass = new TestClass();
    testClass->print(NULL);
    

}

=======================================================
the result should print:

<Types name="Knowledge Types" language="English"><Type id="1"><Name>Information Technology</Name><Lv>1</Lv><UpLvId></UpLvId><Type/><Type id="2"><Name>Music</Name><Lv>1</Lv><UpLvId></UpLvId><Type/><Type id="3"><Name>Health</Name><Lv>1</Lv><UpLvId></UpLvId><Type/><Type id="4"><Name>Business</Name><Lv>1</Lv><UpLvId></UpLvId><Type/><Type id="5"><Name>Spirital</Name><Lv>1</Lv><UpLvId></UpLvId><Type/><Types>



end constructor!!

constructor

NULL

But it obmitted:
constructor

NULL

if comment out //XmlUtils *xmlUtils = new XmlUtils();
then it prints:
constructor

NULL

---------------------------------------------------------
what is the problem of XmlUtils *xmlUtils = new XmlUtils(); ?

Any expert can help? Thanks a lot!!

Man Pak Hong, Dave
manpakhong@hotmail.com


Last edited on
Hi & welcome to cplusplus :+D

To make things easier for everyone, please use code tags:

http://www.cplusplus.com/articles/jEywvCM9/


First up, I am by no means an expert :+) Hopefully I can still give some advice that may be a little bit helpful.

My first instinct is to give the general advice of NOT using new or delete. Use RAII instead by way of smart pointers. There are situations where use of new is warranted, but that is for low level memory management, as far as I understand it. You are using exceptions - a problem there is when an exception is thrown, the code may never reach the delete.

Apart from that, for the default constructor shouldn't it be:

XmlUtils *xmlUtils = new XmlUtils; // without the parentheses

Also, wondering why you use the this pointer so much? It is not necessary to access member variables, but it is needed when overloading operators.

Your constructors aren't initialising all the member variables - you should do this with an initialiser list, instead of all those set functions.

Your aren't calling your constructor for XmlUtils , so none of those append statements are happening, and no member variable is initialised to anything useful. Instead the implicitly created constructor is called. So what you need is to call the constructor that takes a std::string as an argument.

One should sanitise any string entered by the user, otherwise it is a huge security hole. I am sure this is the case for html, and fairly sure it is the same for xml.

I would be wary of names like XmlUtils and xmlUtils which only differ in the case of the first character.

Any way good luck, hopefully my advice has been a little helpful - if not there are plenty here who are way, way more advanced than me.
found the solution myself:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <iostream>
#include <string>
#include <list>

using namespace std;
class Type{
    protected:
        int id;
        string name;
        int lv;
        int upLvId;
    public:
        int getId();
        int setId(int id);
        string getName();
        int setName(std::string name);
        int getLv();
        int setLv(int lv);
        int getUpLvId();
        int setUpLvId(int id);
        Type();
        ~Type();
};
Type::Type(){
    this->id = -1;
    this->name = "";
    this->lv = -1;
    this->upLvId = -1;
}
int Type::setLv(int lv){
    this->lv = lv;
    return 0;
}
int Type::getLv(){
    return lv;
}
int Type::setUpLvId(int id){
    this->id = id;
    return 0;
}
int Type::getUpLvId(){
    return this->upLvId;
}
int Type::setId(int id){
    this->id = id;
    return 0;
}
int Type::getId(){
    return this->id;
}

int Type::setName(std::string name){
    this->name = name;
    return 0;
}
std::string Type::getName(){
    return this->name;
}
Type::~Type(){
    delete this;
}

class XmlUtils{
    public:
        XmlUtils();
        int parseType();
        int print();
        ~XmlUtils();
    private:
        string xmlString;
        int init(string* xmlString);        
};
XmlUtils::XmlUtils(){
    string tst = "test";
    init(&tst);
}

int XmlUtils::init(string* xmlString){
    try{
        if (xmlString == NULL){
            this->xmlString = "<Types name=\"Knowledge Types\" language=\"English\">";
            this->xmlString.append("<Type id=\"1\">");
            this->xmlString.append("<Name>Information Technology</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"2\">");
            this->xmlString.append("<Name>Music</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"3\">");
            this->xmlString.append("<Name>Health</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"4\">");
            this->xmlString.append("<Name>Business</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Type id=\"5\">");
            this->xmlString.append("<Name>Spirital</Name>");
            this->xmlString.append("<Lv>1</Lv>");
            this->xmlString.append("<UpLvId></UpLvId>");
            this->xmlString.append("<Type/>");
            this->xmlString.append("<Types>");
        } else {
            this->xmlString = *xmlString;
        }
        cout << this->xmlString << endl;
        cout << endl;
    } catch (exception &e){
        cout << e.what() << '\n';
    }
    cout << "end constructor!!" << endl;
    return 0;
}
int XmlUtils::print(){
    cout << "print" << endl;
    return 0;
}
int XmlUtils::parseType(){
    cout << "xmlString: " << endl;
    return 0;
}
XmlUtils::~XmlUtils(){
    delete &xmlString;
    delete this;
};

class TypeBuilder{
    private:
        list<Type> engTypeList;
        list<Type> chiTypeList;
        XmlUtils* xmlUtils;
    public:
        TypeBuilder();
        int print();
        list<Type> getInstanceOfEngTypeList();
        list<Type> getInstanceOfChiTypeList();
        ~TypeBuilder();

};
TypeBuilder::TypeBuilder(){
    cout << "constructor" << endl;
    this->xmlUtils = new XmlUtils();
}
int TypeBuilder::print(){
    cout << "TypeBuilder::print()" << endl;
    return 0;
}
list<Type> TypeBuilder::getInstanceOfEngTypeList(){
    try{
        cout << "before xmlUtils" << endl;
        cout << "after xmlUtils " << endl;
       // this->xmlUtils->parseType();
        
    } catch (exception &e){
        cout << "TypeBuilder::getInstanceOfEngTypeList() - " << endl;
        cout << e.what() << '\n';
    }
    return engTypeList;
}
list<Type> TypeBuilder::getInstanceOfChiTypeList(){
    try{
    } catch (exception &e){
        cout << e.what() << '\n';
    }
    return chiTypeList;
}
TypeBuilder::~TypeBuilder(){
    cout << "destructor" << endl;
    delete this->xmlUtils;
    delete this;
}


class TestClass{
    private:
        int id;
        std::string name;
        int lv;
        int upLvId;
        list<Type> engTypeList;
        XmlUtils* xmlUtils;
    public:
        TestClass();
        int getId();
        void setId(int id);
        std::string getName();
        void setName(std::string name);
        int getLv();
        void setLv(int lv);
        int getUpLvId();
        void setUpLvId(int id);
        int print(string* test);
        ~TestClass();
};

TestClass::TestClass() {
    cout << "constructor" << endl;
    this->id = 3;
}

int TestClass::print(string* test){
    try{
        if (test == NULL){
            cout << "NULL" << endl;
        } else {
            cout << *test << endl;
        }
    } catch (exception &e){
        cout << e.what() << '\n';
    }
    return 0;
}
void TestClass::setLv(int lv){
    this->lv = lv;
}
int TestClass::getLv(){
    return this->lv;
}
void TestClass::setUpLvId(int id){
    this->id = id;
}
int TestClass::getUpLvId(){
    return this->upLvId;
}
void TestClass::setId(int id){
    this->id = id;
}
int TestClass::getId(){
    return this->id;
}
void TestClass::setName(std::string name){
    this->name = name;
}
std::string TestClass::getName(){
    return this->name;
}
TestClass::~TestClass(){
    delete this;
}



int main()
{
    //TypeBuilder *typeBuilder = new TypeBuilder();
    // typeBuilder->getInstanceOfEngTypeList();
    //typeBuilder->print();
    //delete typeBuilder;
    //std::cout << "object type -> name: " + typeBuilder->getInstanceOfEngTypeList();
    XmlUtils *xmlUtils = new XmlUtils();
    xmlUtils->print();
    TestClass *testClass = new TestClass();
    testClass->print(NULL);
    

}
Topic archived. No new replies allowed.