Я недавно изучал C++, исходя из фона в Java и других языках высокого уровня. У меня возникли проблемы с указателями и объектами, и мне еще предстоит найти хороший ресурс, который объясняет, как манипулировать объектами, созданными с помощью пользовательских классов, с помощью указателей.Взаимодействие между указателями и классами
Я написал простую программу, чтобы попытаться сузить, где моя проблема.
Последние два блока кода внизу этого класса ошибочны, и я бы поспорил, что я также сделал некоторые вещи неправильно в других частях. Любая помощь приветствуется.
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;
class Point {
public:
float x;
float y;
public:
Point(float x, float y) {
this->x=x;
this->y=y;
}
Point() {
x=0.0f;
y=0.0f;
}
~Point() {
x=0.0f;
y=0.0f;
}
};
class Place {
public:
string name;
Point location;
public:
//Constructor with arguements.
Place(string nam, Point loc) {
name = nam;
location = loc;
}
//Default constructor.
Place() {
name = "Default";
location = {0.0f,0.0f};
}
//Destructor. This object contains no pointers so nothing to delete.
~Place() {
}
public:
void setName(string nam) {
name = nam;
}
void setLocation(Point loc) {
location = loc;
}
};
class PointerToAPlace {
public:
Place *place;
public:
PointerToAPlace(Place *pl) {
*place = *pl;
}
~PointerToAPlace() {
delete place;
}
};
int main(int argc, char** argv) {
std::cout << "\n";
std::cout << "Make some places and hope they don't segfault.\n";
/*Object created with default constructor. Will be automatically deleted at the end of this scope.*/
Place placeA;
std::cout << "Place named " << placeA.name.c_str() << " is at " << placeA.location.x << "," << placeA.location.y << " and is lovely.\n";
//Output: Place named Default is at 0,0 and is lovely.
/*Object created with bracket constructor. Will be automatically deleted at the end of this scope.*/
Place placeB = {"Lordran", Point{12.2f,99.3f}};
std::cout << "Place named " << placeB.name.c_str() << " is at " << placeB.location.x << "," << placeB.location.y << " and is lovely.\n";
//Output: Place named Lordran is at 12.2,99.3 and is lovely.
/*Object created with new keyword. This is a pointer and must be explicitly deleted.*/
Place *placeC = new Place("Drangleic", Point{-123.34f,69.69f});
std::cout << "Place named " << placeC->name.c_str() << " is at " << placeC->location.x << "," << placeC->location.y << " and is lovely.\n";
delete placeC;
//Output: Place named Drangleic is at -123.34,69.69 and is lovely.
/*Array of objects, created with default constructor and not assigned any values. Will be automatically deleted at the end of this scope.*/
Place placeD[3];
std::cout << "Here are some places named: ";
for(int i=0;i<3;i++) {
std::cout << placeD[i].name.c_str();
std::cout << (i<2 ? ", " : ". ");
}
std::cout << "They are quite the places!\n";
//Output: Here are some places named: Default, Default, Default. They are quite the places!
/*Array of objects, created with default constructor and then assigned values. Will be automatically deleted at the end of this scope.*/
Place placeE[5];
string namesA[5] = {"Boletaria", "Stonefang", "Latria", "Shrine of Storms", "Valley of Defilement"};
for(int i=0;i<5;i++) {
placeE[i].setName(namesA[i]);
placeE[i].setLocation(Point{1.23f, 3.21f});
}
std::cout << "Here are some places named: ";
for(int i=0;i<5;i++) {
std::cout << placeE[i].name.c_str();
std::cout << (i<4 ? ", " : ". ");
}
std::cout << "They are quite the places!\n";
//Output: Here are some places named: Boletaria, Stonefang, Latria, Shrine of Storms, Valley of Defilement. They are quite the places!
/*Trying to do a pointer to an array of objects.*/
Place *placeF[7];
string namesB[7] = {"Astora", "Carim", "Thoroughland", "Catarina", "Baulder", "Vinheim", "Zena"};
for(int i=0;i<7;i++) {
placeF[i]->setName(namesB[i]);
placeF[i]->setLocation(Point{2.23f, 6.21f});
}
std::cout << "Here are some places named: ";
for(int i=0;i<7;i++) {
std::cout << placeF[i]->name.c_str();
std::cout << (i<6 ? ", " : ". ");
}
std::cout << "They are quite the places!\n";
//Output: SEGFAULT D:
/*Trying to put a pointer to a place into an object, then delete it.*/
Place *placeG = new Place("Anor Londo", Point{-12312.34f,33.69f});
PointerToAPlace pnt = {placeG};
std::cout << "Place named " << pnt.place->name.c_str() << " is at " << pnt.place->location.x << "," << pnt.place->location.y << " and is lovely.\n";
//Output: SEGFAULT :(
return 0;
}
Пожалуйста, задайте конкретный вопрос. –
Что вы думаете? –
Прежде всего, это даже не нужно компилировать. 'namesB' имеет слишком много строк в инициализаторе. Во всяком случае, вы используете неинициализированные указатели, как если бы они указывали на действительный объект.Я также хотел бы рассмотреть возможность публикации на http://codereview.stackexchange.com советов о том, как лучше использовать язык (например, бесполезный деструктор, который у вас есть для «Point»). – chris