访问构造函数内的信息

时间:2014-03-26 22:32:56

标签: c++

示例类....

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?
    }
}

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

2 个答案:

答案 0 :(得分:0)

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

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<std::shared_ptr<Student> >,其中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

答案 1 :(得分:0)

您需要将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;
}