【问题标题】:Problems with default value of pointer to pointer in constructor构造函数中指向指针的默认值的问题
【发布时间】:2015-03-27 17:02:42
【问题描述】:

当我们使用指向类的“双”指针时,我们在构造函数中用参数写了什么?我们是否使用一个指针来分配内存?

这里是代码。它无法编译,我不明白为什么。感谢您的帮助。

#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

class Article{
private:
    char title[100];
    char author[50];
    char *content;
    bool publish;
public:
    Article(char *title="", char *author="", char *content="", bool publish=0){
    strcpy(this->title, title);
    strcpy(this->author, author);
    this->content=new char[strlen(content)+1];
    strcpy(this->content, content);
    this->publish=publish;
    }
    void show(){
        cout<<title<<endl;
        cout<<author<<endl;
        cout<<content<endl;
    }
    ~Article(){
        delete [] content;
    }
};

class Newspaper{
private:
    char name[100];
    Article **p;
    int articles;
    Article first;
public:
    Newspaper(char *name="", Article **p=Article(), int articles=0, Article first=Article()){
        strcpy(this->name, name);
    }

};

int main() {
    char title[100], author[50], content[100];
    int n;
    cin >> n;
    char name[100];
    cin.getline(name, 100);
    cin.getline(name, 100);

    Article first("VAZNO","OOP","Vezba:OOP",true);
    Newspaper v(name,first);
    Article **s = new Article*[n];
    for(int i = 0; i < n; ++i) {
        cin.getline(title, 100);
        cin.getline(author, 50);
        cin.getline(content, 100);
        v.addArticle(Article(title, author, content, true)); //se koristi copy konstruktor
    }
    v.showFirst();
    v.showLongest();
    cout << v.totalofauthor(author) << endl;
    for(int i = 0; i < n; ++i) {
        delete s[i];
    }
    delete [] s;
        return 0;

    }

【问题讨论】:

  • pastebin.com/cy6p6Pf4 这是代码 :)
  • 请从该代码创建一个最小示例并将其放入问题中
  • 如果你只使用std::stringstd::vector,你可能会解决你所有的错误,还有更多你还没有找到的错误
  • 您的语法不正确:Article **p=Article()。您想要其中之一:Article p = Article()Article *p = new ArticleArticle * * p = 0。最好不要使用动态内存,因为内存管理很困难(何时删除指针,谁拥有目标,有多少指针指向对象等)。
  • 一个最小的例子。伙计,现在选择性阅读风靡一时!

标签: c++ class pointers constructor


【解决方案1】:

您的问题的解决方案:

从您的代码看来,Newspaper 似乎使用指向指针的指针(您称之为双指针)来跟踪 Articles

    Newspaper(char *name, Article **p ,                       // This would be ok
                 int articles = 0, Article first = Article())  

但是您很难为其定义默认参数:

    Newspaper(char *name="", Article **p=Article(),          // oops doesn't compile 
                 int articles = 0, Article first = Article())  

原因是“双”指针与对象本身不同。如果你想要一个默认参数,你也必须提供一个双指针。

这段代码正是这样做的:

    Newspaper(char *name = "", Article **p = new Article*,    // this is ok !
                 int articles = 0, Article first = Article()){

所以这就是你的问题的解决方案。

但是你的问题是什么?

但是目的是什么或提供一个虚拟指针,指向无处作为默认参数?

稍后您再次尝试根据一篇文章创建Newspaper

    Newspaper v(name, first);   // won't work, because first is not a pointer either 

所以问题不在于构造函数,而在于整个原理。看来您确实想基于Article 创建Newspaper,并且您使用默认值来涵盖有人想要在没有Article 的情况下创建Newspaper 的情况。

显然您的设计还预见到Articles 可以动态添加:

    v.addArticle(Article(title, author, content, true)); 

最后看来你真的遇到了指针的问题:在你写的构造函数中,你没有将指针初始化为足够大的内存区域来保存名称:

 Newspaper(char *name = "", ...) { 
    strcpy(this->name, name);    // Ouch !!  pointer name is not initalized !  
 }

所以一旦你编译了你的代码,你的程序就不能工作了!由于this-&gt;name 是一个从未初始化的指针,您的strcpy() 将导致内存损坏和未定义行为(崩溃!?)。

建议

首先,获取一本书或一个教程来理解和掌握指针。如果你不这样做,你很快就会完全迷失在你的 C++ 课程中。

同时,摆脱您的char*strcpy() 等,改用std::string

最后,考虑使用 std::vector 来管理动态的Article 容器。使用Article** 实现的动态指针数组将需要额外的逻辑,例如在文章数量增加时维护大小、重新分配内存,而不是说您放入数组中的文章的所有权(分配/解除分配)。

根据这些建议,您的 Newspaper 将如下所示:

class Newspaper{
private:
    string name;
    vector<Article> p;   // p.size() will provide the number of articles 

public:
    Newspaper(string name = "") : name(name) { }  // empty newspaper 
    Newspaper(string name, Article& a) : Newspaper(name) {  // combo
        addArticle(a); 
        }
    void addArticle(Article &a) {
        p.push_back(a);                // add and article to the vector
    }
    ... // rest of the code, here
};

还有here 多一点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 2013-03-28
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    相关资源
    最近更新 更多