So what I am trying to do, I have two classes named ClassA and ClassB, in ClassB there exists a pointer which is referring to an array of ClassA objects. But when I am trying to save the objects inside the array the details are not getting reflected. I tried debugging the same and it's giving me segmentation fault. Is there anything I am doing wrong?
Classes File:
#include <bits/stdc .h>
using namespace std;
class ClassA{
private:
string name;
string phone;
public:
ClassA(string name, string phone)
{
this->name = name;
this->phone = phone;
}
string getName()
{
return name;
}
string getPhone()
{
return phone;
}
};
class ClassB{
private:
ClassA* details;
int size;
public:
ClassB()
{
size = 0;
details = (ClassA*)malloc(5*sizeof(ClassA));
}
void addDetails(string name, string phone)
{
ClassA* temp = new ClassA(name, phone);
details[size ] = *temp;
// cout<<details[size-1].getName()<<" "<<details[size-1].getPhone()<<endl;
}
void print()
{
for(int i = 0; i < size; i )
{
cout << details[i].getName() << " " << details[i].getPhone() << endl;
}
}
};
Driver Code:
#include "temp.h"
using namespace std;
int main()
{
ClassB b;
b.addDetails("A", "123");
b.addDetails("B", "456");
b.addDetails("C", "789");
b.print();
return 0;
}
Anyone, please help me out on this.
CodePudding user response:
When you are calling malloc
you won't call the constructor, so ClassA is not constructed.
Plus, your malloc put only allocate a place for one object.
You better use std::vector
, see the example below.
#include <iostream>
#include <vector>
using namespace std;
class ClassA{
private:
string name;
string phone;
public:
ClassA(string name, string phone)
{
this->name = name;
this->phone = phone;
}
string getName()
{
return name;
}
string getPhone()
{
return phone;
}
};
class ClassB{
private:
std::vector<ClassA> details;
public:
void addDetails(string name, string phone)
{
details.emplace_back(name, phone);
//cout<<details[details.size()-1].getName()<<" "<<details[details.size()-1].getPhone()<<endl;
}
void print()
{
for(int i = 0; i < details.size(); i )
{
cout << details[i].getName() << " " << details[i].getPhone() << endl;
}
}
};
int main()
{
ClassB b;
b.addDetails("A", "123");
b.addDetails("B", "456");
b.addDetails("C", "789");
b.print();
return 0;
}
CodePudding user response:
You are mixing malloc
and new
, which causes undefined behavior.
The C library function malloc()
does not initialize the memory nor call the initializer of the class
, since C
does not have classes. That's why you should never call malloc()
to allocate heap memory for classes/structs
in C
. Whereas new
is a operator which returns the exact data type and calls the initializer of the class\struct
.
Some Problems:
- Use
size_t
to store length of the array. - Use
std::vector
to store unknown amount of similar data type. - If parameter is not going to change inside the function, write it as
const std::string &
- do not include
<bits/stdc .h>
- do not write this
using namespace std;
So, you final code should be TRY IT ONLINE
:
#include <iostream>
#include <vector>
class ClassA{
private:
std::string name;
std::string phone;
public:
ClassA(const std::string &name, const std::string &phone)
{
this->name = name;
this->phone = phone;
}
std::string getName()
{
return this->name;
}
std::string getPhone()
{
return this->phone;
}
};
class ClassB{
private:
std::vector<ClassA> vec;
public:
ClassB() = default;
void addDetails(const std::string &name, const std::string &phone)
{
vec.push_back(ClassA(name, phone));
}
void print()
{
for (auto &&i : vec){
std::cout << i.getName() << " " << i.getPhone() << "\n";
}
std::cout << std::endl;
}
};
int main(void)
{
ClassB b;
b.addDetails("A", "123");
b.addDetails("B", "456");
b.addDetails("C", "789");
b.print();
return 0;
}