访问构造函数内的信息 [英] Access information inside Constructors

查看:152
本文介绍了访问构造函数内的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

范例class ....

Example class....

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

如何访问构造函数中的字符串数组?

How do I access the array of strings inside the constructor? I want to put the course names inside the constructor.

推荐答案

CName 变量只在您的构造函数的本地范围中声明mehtod:

The CName variable is only declared in the local scope of your constructor mehtod:

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

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

你可能意味着它是一个类成员变量。还要注意,不需要 string * 指针或,只需使用 std: :vector

You probably meant to have it being a class member variable. Also note there's no need for a string* pointer or new, just use a std::vector instead:

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 [,随后称为 / code>

After this constructor was called, you can access StudentNames[i] where i is in the range of [0-AmtClass[, in subsequently called methods from your Student class.

所有这一切,这也建议命名你的类学生以正确反映预期的复数语义。

或者更好的是,这应该命名为 Class 持有 std :: vector< std :: shared_ptr< Student> > ,其中学生应该有 std :: string c $ c> name

All of that said, this would also suggest to name your class Students to reflect the intended plural semantics correctly.
Or even better, this should be named Class holding a std::vector<std::shared_ptr<Student> >, where Student is supposed to have a std::string member name.

OK,我们去:

#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

这篇关于访问构造函数内的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆