gpt4 book ai didi

c++ - 我不知道如何处理我的非默认构造函数中传递的指针

转载 作者:行者123 更新时间:2023-11-28 00:54:33 26 4
gpt4 key购买 nike

我不知道我应该如何处理我的非默认构造函数中传递的对象指针。我做这个程序是错误的,因为我觉得我不知道自己在做什么。

我已经在这个问题上卡了一天半了,我就是不明白。我将在必须使用默认构造函数和非默认构造函数的地方发布我的大部分作业。我还将包括我正在使用的类图,以便您了解变量/类/方法的功能。

我应该如何处理非默认构造函数内部传递的指针?我不能通过这里分配任何值,因为它会告诉我它不可访问。在这段代码中:

Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
setTitle(title);
setPrice(price);
}

知道如何使用这些指针吗?现在我可以使用默认构造函数正确传递所有内容,但我可能做错了,因为我正在使用其他对象。基本上我不应该有 2 行 Author author;Publisher publisher; 但我现在不在乎。

请帮忙!

图书.cpp文件

#include <iostream>
#include <sstream>
using namespace std;

#include "Book.h"
#include "Publisher.h"
#include "Author.h"

class Book
{
public:
Book();
Book(string title, Author *pAuthor, Publisher *pPublisher, double price);
~Book();
void setTitle(string title);
void setAuthorName(string first, string last);
void setPublisher(string name, string address, string city);
void setPrice(double price);
string convertDoubleToString(double number);
string getBookInfo();

private:
string title;
double price;
Author *pAuthor;
Publisher *pPublisher;

Author author;
Publisher publisher;
};

Book::Book()
{
}

Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
setTitle(title);
setPrice(price);
}

Book::~Book()
{
}

void Book::setTitle(string title)
{
this->title = title;
}

void Book::setAuthorName(string first, string last)
{
author.setFirstName(first);
author.setLastName(last);
}

void Book::setPublisher(string name, string address, string city)
{
publisher.setName(name);
publisher.setAddress(address);
publisher.setCity(city);
}

void Book::setPrice(double price)
{
this->price = price;
}

string Book::convertDoubleToString(double number)
{
return static_cast<ostringstream*>( &(ostringstream() << number) ) -> str();
}

string Book::getBookInfo()
{
return title + "\n" + author.getFullName() + "\n" + publisher.getPublisherInfo() + "\n" + "$" + convertDoubleToString(price);
}

main.cpp 文件:

#include <iostream>
#include <string>
using namespace std;

#include "Book.h"

int main()
{
system("cls");

cout << "Book 1" << endl;

Author *pAuthor = new Author("John", "Doe");
Publisher *pPublisher = new Publisher("Wrox", "10475 Crosspoint Blvd.", "Indianapolis");
Book *pBook = new Book("Memory Management", pAuthor, pPublisher, 39.99);

cout << pBook->getBookInfo() << endl;

cout << endl << "Book 2" << endl;

Book book;

book.setTitle("Advanced C++ Programming");
book.setAuthorName("Linda", "Smith");
book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond");
book.setPrice(49.99);

cout << book.getBookInfo() << endl << endl;

system("pause");

return 0;
};

Author.cpp 文件:

#include <iostream>
#include <string>
using namespace std;

class Author
{
public:
Author();
Author(string first, string last);
string getFullName();
void setFirstName(string first);
void setLastName(string last);

private:
string firstName;
string lastName;
};

Author::Author()
{
}

Author::Author(string first, string last)
{
setFirstName(first);
setLastName(last);
}

string Author::getFullName()
{
return firstName + " " + lastName;
}

void Author::setFirstName(string first)
{
this->firstName = first;
}

void Author::setLastName(string last)
{
this->lastName = last;
}

类图:

enter image description here

最佳答案

除非赋值要求你使用指针数据成员,否则最好的解决方案是简单地将它们一起删除,并修改相关的构造函数,你也可以通过使用初始化列表来改进:

class Book {

....
public:
Book(std::string title, const Author& author, const Publisher& publisher, double price);
private:
// use some convention for private data member names, here trailing _
std::string title_;
double price_;
Author author_;
Publisher publisher_;

};

Book::Book(string title, const Author& author, const Publisher& publisher, double price)
: title_(title), price_(price), author_(author), publisher_(publisher)
{

}

编辑:因为它似乎是使用指针的要求,而且因为 Book 类有尝试修改 Author 的方法和 Publisher,您需要对输入指针指向的对象进行深度复制。这意味着使用 new 动态分配对象并将指向它的指针分配给您的指针数据成员之一。例如,

class A { .... }; // class with copy constructor

A* a1; // pointer to an A, points to nothing.
a = new A(); // a1 now points to dynamically allocated A. You are in charge of deleting!
A* a2(new A); // pointer to an A, initialized to point to dynamically allocated A.

但是您负责删除动态分配的对象,并且必须修改您的类以使复制构造和分配合理,即遵循rule of three .希望您会看到在这种特殊情况下使用指针会导致比其值(value)更多的问题。

关于c++ - 我不知道如何处理我的非默认构造函数中传递的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com