【问题标题】:Access information inside Constructors访问构造函数内部的信息
【发布时间】:2014-03-26 22:32:55
【问题描述】:

示例类....

class Student
{
    private: 
        int AmtClass;
    public:
        Student(int AmtClass);
}


Student::Student(int amount)
{
    AmtClass = amount;

    string *CName;
    CName = new string[AmtClass];
}

int main()
{
    int amount;
    string Names[amount];
    cin >> amount;
    Student stud(amount);

    for(int i = 0; i > amount; i++)
    {
        getline(cin,Names[i]);
        //How can I access the constructor from here?
    }
}

如何访问构造函数中的字符串数组?我想把课程名称放在构造函数中。

【问题讨论】:

  • 请提交实际编译的代码,这样我们就可以开始了。
  • 我强烈建议您开始使用std::vector 而不是new[]

标签: c++


【解决方案1】:

您需要将 pName 声明为类的成员变量。然后您可以在获取输入时将输入存储在 pName 数组中。

class Student
{
    public:
        int AmtClass;
        string *pName;

        Student(int AmtClass);
};


Student::Student(int amount)
{
    AmtClass = amount;
    pName = new string[AmtClass];
}

int main()
{
    int amount;
    string Names;
    cin >> amount;
    getline(cin,Names);
    Student stud(amount);


    for(int i = 0; i < amount; i++)
    {
        getline(cin,Names);
        stud.pName[i] = Names;
        //How can I access the constructor from here?
    }
    return 0;
}

【讨论】:

  • 是的,我试图让它接近原始代码。用户可以得到这个想法。他可以随心所欲地使用。
  • 在这种情况下,您至少应该通过rule-of-three 扩展您的答案样本以完全正确,并且不会从丢失的析构函数中泄漏内存!跨度>
【解决方案2】:

CName 变量仅在构造函数方法的本地范围内声明:

Student::Student(int amount)
{
    AmtClass = amount;

    string *CName; // <<<
    pName = new string[AmtClass]; // <<< Just leaks memory, nothing else
}

你可能打算让它成为一个类成员变量。另请注意,不需要string* 指针或new,只需使用std::vector

class Student
{
private: 
    int AmtClass;
    std::vector<std::string> > StudentNames;
public:
    Student(int numStudents);
}

Student::Student(int numStudents) 
: AmtClass(numStudents)
{
    StudentNames.resize(AmtClass);
}

调用此构造函数后,您可以访问StudentNames[i],其中i 在[0-AmtClass[ 的范围内,在随后从您的Student 类中调用的方法中。

综上所述,这也建议将您的类命名为 Students 以正确反映预期的复数语义。
或者更好的是,它应该被命名为Class 持有一个std::vector&lt;std::shared_ptr&lt;Student&gt; &gt;,其中Student 应该有一个std::string 成员name

OK, here we go:

#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <algorithm>

class Student {
private:
    std::string name;

public:
    Student(const std::string& name_) : name(name_) {}
    const std::string& getName() const { return name; }
};

class Class {
private:
    typedef std::vector<std::shared_ptr<Student>> SubscriptionList;
    SubscriptionList students;
    const unsigned int MaxSubscribers;

public:
    Class(unsigned int maxSubscribers) : MaxSubscribers(maxSubscribers) {}

    bool subscribe(std::shared_ptr<Student>& student) {
        if(students.size() >= MaxSubscribers) {
            return false; // Class is full, can't subscribe
        }

        // May be put additional checks here, to prevent multiple subscrptions
        students.push_back(student);
        return true; // Sucessfully subscribed the class
    }

    void unsubscribe(std::shared_ptr<Student>& student) {
        SubscriptionList::iterator it = students.begin();
        for(;it != students.end();++it) {
            if(it->get() == student.get()) {
                break;
            }
        }
        students.erase(it);
    }

    void showSubscribedStudents(std::ostream& os) {
        unsigned int i = 1;
        for(SubscriptionList::iterator it = students.begin();
            it != students.end();
            ++it, ++i) {
            os << i << ". " << (*it)->getName() << std::endl;
        }
    }
};

int main()
{
    unsigned int amount;
    std::cin >> amount;
    std::cin.ignore();

    Class theClass(amount);

    for(unsigned int i = 0; i < amount; i++)
    {
        std::string name;
        std::getline(std::cin,name);
        std::shared_ptr<Student> student(new Student(name));
        theClass.subscribe(student);
    }

    theClass.showSubscribedStudents(std::cout);
}

输入:

5
John Doe
Jane Doe
Aldo Willis
Chuck Norris
Mia Miles

输出:

1. John Doe
2. Jane Doe
3. Aldo Willis
4. Chuck Norris
5. Mia Miles

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多